NumPy创建数组
创建方法概述
ones和zeros填充方式
方法 | 描述 |
---|---|
empty(shape[, dtype, order]) | 返回给定形状和类型的新数组,元素是随机的 |
empty_like(prototype[, dtype, order, subok, …]) | 返回形状和类型与给定数组相同的新数组,元素是随机的 |
eye(N[, M, k, dtype, order]) | 返回一个二维数组,对角线上有一个,其他地方为零。 |
identity(n[, dtype]) | 返回单位矩阵 |
ones(shape[, dtype, order]) | 返回给定形状和类型的新数组,并填充为1。 |
ones_like(a[, dtype, order, subok, shape]) | 返回形状与类型与给定数组相同的数组。 |
zeros(shape[, dtype, order]) | 返回给定形状和类型的新数组,并用零填充。 |
zeros_like(a[, dtype, order, subok, shape]) | 返回形状与类型与给定数组相同的零数组。 |
full(shape, fill_value[, dtype, order]) | 返回给定形状和类型的新数组,并用fill_value填充。 |
full_like(a, fill_value[, dtype, order, …]) | 返回形状和类型与给定数组相同的完整数组。 |
从现有数据创建
方法 | 描述 |
---|---|
array(object[, dtype, copy, order, subok, ndmin]) | 创建一个数组。 |
asarraya[, dtype, order]) | 将输入转换为数组。 |
asanyarray(a[, dtype, order]) | 将输入转换为ndarray,但通过ndarray子类。 |
ascontiguousarray(a[, dtype]) | 返回内存中的连续数组(ndim > = 1)(C顺序)。 |
asmatrix(data[, dtype]) | 将输入解释为矩阵。 |
copy(a[, order]) | 返回给定对象的数组副本。 |
frombuffer(buffer[, dtype, count, offset]) | 将缓冲区解释为一维数组。 |
fromfile(file[, dtype, count, sep, offset]) | 根据文本或二进制文件中的数据构造一个数组。 |
fromfunction(function, shape, **kwargs) | 通过在每个座标上执行一个函数来构造一个数组。 |
fromiter(iterable, dtype[, count]) | 从可迭代对象创建一个新的一维数组。 |
fromstring(string[, dtype, count, sep]) | 从字符串中的文本数据初始化的新一维数组。 |
loadtxt(fname[, dtype, comments, delimiter, …]) | 从文本文件加载数据。 |
创建记录数组
方法 | 描述 |
---|---|
core.records.array(obj[, dtype, shape, …]) | 从各种各样的对象构造一个记录数组。 |
core.records.fromarrays(arrayList[, dtype, …]) | 从(平面)数组列表创建记录数组 |
core.records.fromrecords(recList[, dtype, …]) | 从文本格式的记录列表创建一个rearray |
core.records.fromstring(datastring[, dtype, …]) | 根据字符串中包含的二进制数据创建(只读)记录数组 |
core.records.fromfile(fd[, dtype, shape, …]) | 根据二进制文件数据创建数组 |
创建字符数组
方法 | 描述 |
---|---|
core.defchararray.array(obj[, itemsize, …]) | 创建一个chararray。 |
core.defchararray.asarray(obj[, itemsize, …]) | 将输入转换为chararray,仅在必要时复制数据。 |
数值范围
方法 | 描述 |
---|---|
arange([start,] stop[, step,][, dtype]) | 返回给定间隔内的均匀间隔的值。 |
linspace(start, stop[, num, endpoint, …]) | 返回指定间隔内的等间隔数字。 |
logspace(start, stop[, num, endpoint, base, …]) | 返回数以对数刻度均匀分布。 |
geomspace(start, stop[, num, endpoint, …]) | 返回数字以对数刻度(几何级数)均匀分布。 |
meshgrid(*xi, **kwargs) | 从座标向量返回座标矩阵。 |
mgrid | nd_grid实例,它返回一个密集的多维“ meshgrid”。 |
ogrid | nd_grid实例,它返回一个开放的多维“ meshgrid”。 |
创建矩阵
方法 | 描述 |
---|---|
diag(v[, k]) | 提取对角线或构造对角线数组。 |
diagflat(v[, k]) | 使用展平的输入作为对角线创建二维数组。 |
tri(N[, M, k, dtype]) | 在给定对角线处及以下且在其他位置为零的数组。 |
tril(m[, k]) | 数组的下三角。 |
triu(m[, k]) | 数组的上三角。 |
vander(x[, N, increasing]) | 生成范德蒙矩阵。 |
矩阵类
方法 | 描述 |
---|---|
mat(data[, dtype]) | 将输入解释为矩阵。 |
bmat(obj[, ldict, gdict]) | 从字符串,嵌套序列或数组构建矩阵对象。 |
常用的数组创建方法
使用底层的ndarray构造器来创建
ndarray(shape, dtype=None, buffer=None, offset=0, strides=None, order=None)
参数说明:
参数 | 类型 | 描述 |
---|---|---|
shape | int型tuple | 多维数组的形状 |
dtype | data-type | 数组中元素的类型,可选 |
buffer | 用于初始化数组的buffer | |
offset | int | buffer中用于初始化数组的首个数据的偏移 |
strides | int型tuple | 每个轴的下标增加1时,数据指针在内存中增加的字节数 |
order | ‘C’ 或者 ‘F’ | ‘C’:行优先;‘F’:列优先 |
示例:
a = np.ndarray((2, 3), dtype=int, buffer=np.array([1, 2, 3, 4, 5, 6]), offset=0, order='C')
print(a) # output:[[1 2 3],[4 5 6]]
a = np.ndarray(shape=(2,3), dtype=int, buffer=np.array([1, 2, 3, 4, 5, 6, 7]), offset=0, order="F")
print(a) # output:[[1 3 5], [2 4 6]]
a = np.ndarray(shape=(2,3), dtype=int, buffer=np.array([1, 2, 3, 4, 5, 6, 7]), offset=8, order="C")
print(a) # output:[[2 3 4], [5 6 7]]
使用empty方法创建
与zeros不同,empty没有将数组值设置为0,因此可能会稍微块一些。另一方面,它要求用户手动设置数组中的所有值,并且应该谨慎使用;
empty(shape, dtype = float, order = 'C')
参数说明:
参数 | 描述 |
---|---|
shape | 多维数组的形状 |
dtype | 数组中元素的类型,可选 |
order | ‘C’:行优先;‘F’:列优先 |
实例:
x = np.empty([3,2], dtype=int)
print(x)
# [[ 6917529027641081856 5764616291768666155]
# [ 6917529027641081859 -5764598754299804209]
# [ 4497473538 844429428932120]]
使用zeros方法创建
创建指定大小的数组,数组元素以 0 来填充:
zeros(shape, dtype = float, order = 'C')
参数说明:
参数 | 描述 |
---|---|
shape | 多维数组的形状 |
dtype | 数组中元素的类型,可选 |
order | ‘C’:行优先;‘F’:列优先 |
实例:
# 默认为浮点数
x = np.zeros(5)
print(x) # output:[0. 0. 0. 0. 0.]
# 设置类型为整数
y = np.zeros((5,), dtype = np.int)
print(y) # output:[0 0 0 0 0]
# 自定义类型
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
print(z) # output:[[(0, 0) (0, 0)]
# [(0, 0) (0, 0)]]
使用ones方法创建
创建指定形状的数组,数组元素以 1 来填充:
ones(shape, dtype = None, order = 'C')
参数说明:
参数 | 描述 |
---|---|
shape | 多维数组的形状 |
dtype | 数组中元素的类型,可选 |
order | ‘C’:行优先;‘F’:列优先 |
# 默认为浮点数
x = np.ones(5)
print(x) # output:[1. 1. 1. 1. 1.]
# 自定义类型
x = np.ones([2,2], dtype = int)
print(x) # output:[[1 1]
[1 1]]
使用asarray方法创建
asarray 类似 array,但 asarray 参数只有三个,比 numpy.array 少两个。
asarray(a, dtype = None, order = None)
参数说明:
参数 | 描述 |
---|---|
a | 任意形式的输入参数,可以是,列表, 列表的元组, 元组, 元组的元组, 元组的列表,多维数组 |
dtype | 数据类型,可选 |
order | 可选,有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。 |
实例:
# 将列表转换为 ndarray:
x = [1,2,3]
a = np.asarray(x)
print (a) # output:[1 2 3]
# 将元组转换为 ndarray:
x = (1,2,3)
a = np.asarray(x)
print (a) # output:[1 2 3]
# 将元组列表转换为 ndarray:
x = [(1,2,3),(4,5)]
a = np.asarray(x)
print (a)
输出结果为:
[(1, 2, 3) (4, 5)]
使用frombuffer方法创建
frombuffer 用于实现动态数组。
frombuffer 接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象。
frombuffer(buffer, dtype = float, count = -1, offset = 0)
注意:buffer 是字符串的时候,Python3 默认 str 是 Unicode 类型,所以要转成 bytestring 在原 str 前加上 b。
参数说明:
参数 | 描述 |
---|---|
buffer | 可以是任意对象,会以流的形式读入。 |
dtype | 返回数组的数据类型,可选 |
count | 读取的数据数量,默认为-1,读取所有数据。 |
offset | 读取的起始位置,默认为0。 |
s = b'Hello World'
a = np.frombuffer(s, dtype = 'S1')
print (a) # output:[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']
使用fromiter方法调用
fromiter 方法从可迭代对象中建立 ndarray 对象,返回一维数组。
fromiter(iterable, dtype, count=-1)
参数描述:
参数 | 描述 |
---|---|
iterable | 可迭代对象 |
dtype | 返回数组的数据类型 |
count | 读取的数据数量,默认为-1,读取所有数据 |
实例
# 使用 range 函数创建列表对象
list=range(5)
it=iter(list)
# 使用迭代器创建 ndarray
x=np.fromiter(it, dtype=float)
print(x) # output:[0. 1. 2. 3. 4.]
使用arange方法创建
numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray。函数格式如下:
arange(start, stop, step, dtype)
参数说明:
参数 | 描述 |
---|---|
start | 起始值,默认为0 |
stop | 终止值(不包含) |
step | 步长,默认为1 |
dtype | 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。 |
实例:
# 生成 0 到 5 的数组:
x = np.arange(5)
print (x) # output:[0 1 2 3 4]
# 设置返回类型位 float:
# 设置了 dtype
x = np.arange(5, dtype = float)
print (x) # output:[0. 1. 2. 3. 4.]
# 设置了起始值、终止值及步长:
x = np.arange(10,20,2)
print (x) # output:[10 12 14 16 18]
使用linspace方法创建
linspace 函数用于创建一个一维数组,数组是一个等差数列构成的,格式如下:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数说明:
参数 | 描述 |
---|---|
start | 序列的起始值 |
stop | 序列的终止值,如果endpoint为true,该值包含于数列中 |
num | 要生成的等步长的样本数量,默认为50 |
endpoint | 该值为 true 时,数列中中包含stop值,反之不包含,默认是True。 |
retstep | 如果为 True 时,生成的数组中会显示间距,反之不显示。 |
dtype | ndarray 的数据类型 |
实例
a = np.linspace(1,10,10)
print(a) # output:[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
# 设置元素全部是1的等差数列:
a = np.linspace(1,1,10)
print(a) # output:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
# 将 endpoint 设为 false,不包含终止值:
a = np.linspace(10, 20, 5, endpoint = False)
print(a) # output:[10. 12. 14. 16. 18.]
# 如果将 endpoint 设为 true,则会包含 20。
a =np.linspace(1,10,10,retstep= True)
print(a)
# 拓展例子
b =np.linspace(1,10,10).reshape([10,1])
print(b)
输出结果为:
(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]
[ 6.]
[ 7.]
[ 8.]
[ 9.]
[10.]]
使用logspace方法创建
logspace 函数用于创建一个于等比数列。格式如下:
logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
base 参数意思是取对数的时候 log 的下标。
参数 | 描述 |
---|---|
start | 序列的起始值为:base ** start |
stop | 序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中 |
num | 要生成的等步长的样本数量,默认为50 |
endpoint | 该值为 true 时,数列中中包含stop值,反之不包含,默认是True。 |
base | 对数 log 的底数。 |
dtype | ndarray 的数据类型 |
实例
# 默认底数是 10
a = np.logspace(1.0, 2.0, num = 10)
print (a)
# output:
# [ 10. 12.91549665 16.68100537 21.5443469 27.82559402
# 35.93813664 46.41588834 59.94842503 77.42636827 100. ]
# 将对数的底数设置为 2 :
a = np.logspace(0,9,10,base=2)
print (a) # output:[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]