数组创建例程#

从形状或值#

empty (shape[, dtype, order, device, like])

返回给定形状和类型的新数组,而不初始化条目.

empty_like (prototype[, dtype, order, subok, ...])

返回一个与给定数组具有相同形状和类型的新数组.

eye (N[, M, k, dtype, order, device, like])

返回一个二维数组,对角线上为 1,其他地方为 0.

identity (n[, dtype, like])

返回单位数组.

ones (shape[, dtype, order, device, like])

返回一个给定形状和类型的新数组,填充为 1.

ones_like (a[, dtype, order, subok, shape, ...])

返回一个与给定数组具有相同形状和类型的全 1 数组.

zeros (shape[, dtype, order, like])

返回一个给定形状和类型的新数组,并填充零.

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, ...])

创建数组.

asarray (a[, dtype, order, device, copy, like])

将输入转换为数组.

asanyarray (a[, dtype, order, device, copy, like])

将输入转换为ndarray,但传递ndarray子类.

ascontiguousarray (a[, dtype, like])

返回内存中 (C 顺序) 的连续数组 (ndim >= 1).

asmatrix (data[, dtype])

将输入解释为矩阵.

astype (x, dtype, /, \[, copy, device])

将数组复制到指定的数据类型.

copy (a[, order, subok])

返回给定对象的数组副本.

frombuffer (buffer[, dtype, count, offset, like])

将缓冲区解释为一维数组.

from_dlpack (x, /, \[, device, copy])

从实现 __dlpack__ 协议的对象创建一个 NumPy 数组.

fromfile (file[, dtype, count, sep, offset, like])

从文本或二进制文件中的数据构造数组.

fromfunction (function, shape, \[, dtype, like])

通过对每个坐标执行一个函数来构造一个数组.

fromiter (iter, dtype[, count, like])

从可迭代对象创建一个新的 1 维数组.

fromstring (string[, dtype, count, like])

从字符串中的文本数据初始化的新 1-D 数组.

loadtxt (fname[, dtype, comments, delimiter, ...])

从文本文件加载数据.

创建记录数组#

备注

请参考 记录数组 获取记录数组信息.

rec.array (obj[, dtype, shape, offset, ...])

从各种对象构造记录数组.

rec.fromarrays (arrayList[, dtype, shape, ...])

从(扁平)数组列表创建记录数组

rec.fromrecords (recList[, dtype, shape, ...])

从文本形式的记录列表创建 recarray.

rec.fromstring (datastring[, dtype, shape, ...])

从二进制数据创建记录数组

rec.fromfile (fd[, dtype, shape, offset, ...])

从二进制文件数据创建数组

创建字符数组 ( numpy.char )#

备注

numpy.char 用于创建字符数组.

char.array (obj[, itemsize, copy, unicode, order])

创建一个 chararray .

char.asarray (obj[, itemsize, unicode, order])

将输入转换为 chararray ,仅在必要时复制数据.

数值范围#

arange ([start,] stop[, step,][, dtype, ...])

返回给定区间内的均匀间隔的值.

linspace (start, stop[, num, endpoint, ...])

返回在指定间隔内均匀间隔的数字.

logspace (start, stop[, num, endpoint, base, ...])

返回在对数刻度上均匀间隔的数字.

geomspace (start, stop[, num, endpoint, ...])

返回在对数刻度上均匀分布的数字(几何级数).

meshgrid (xi[, copy, sparse, indexing])

从坐标向量返回坐标矩阵的元组.

mgrid 

一个返回密集多维"meshgrid"的实例.

ogrid 

一个实例,返回一个开放的多维"meshgrid".

构建矩阵#

diag (v[, k])

提取对角线或构造对角线数组.

diagflat (v[, k])

创建一个二维数组,其对角线设置为扁平化的输入.

tri (N[, M, k, dtype, like])

一个数组,在给定的对角线处和下方填充 1,其他地方填充 0.

tril (m[, k])

数组的下三角.

triu (m[, k])

数组的上三角.

vander (x[, N, increasing])

生成Vandermonde矩阵.

矩阵类#

bmat (obj[, ldict, gdict])

从字符串,嵌套序列或数组构建矩阵对象.