数组操作例程#

基本操作#

copyto (dst, src[, casting, where])

将值从一个数组复制到另一个数组,并在必要时进行广播.

ndim (a)

返回数组的维度数.

shape (a)

返回数组的形状.

size (a[, axis])

返回沿给定轴的元素数.

更改数组形状#

reshape (a, /[, shape, order, newshape, copy])

在不更改数据的情况下为数组赋予新的形状.

ravel (a[, order])

返回一个连续的扁平化数组.

ndarray.flat 

一个在数组上进行 1-D 迭代的迭代器.

ndarray.flatten ([order])

返回一个折叠成一维的数组的副本.

类似转置的操作#

moveaxis (a, source, destination)

将数组的轴移动到新位置.

rollaxis (a, axis[, start])

向后滚动指定的轴,直到它位于给定的位置.

swapaxes (a, axis1, axis2)

交换数组的两个轴.

ndarray.T 

转置数组的视图.

transpose (a[, axes])

返回轴转置的数组.

permute_dims (a[, axes])

返回轴转置的数组.

matrix_transpose (x, /)

转置矩阵(或矩阵堆栈) x .

更改维度数#

atleast_1d (arys)

将输入转换为至少一维的数组.

atleast_2d (arys)

将输入视为至少二维的数组.

atleast_3d (arys)

将输入视为至少三维的数组.

broadcast 

生成一个模仿广播的对象.

broadcast_to (array, shape[, subok])

将数组广播到新形状.

broadcast_arrays (args[, subok])

将任意数量的数组相互广播.

expand_dims (a, axis)

扩展数组的形状.

squeeze (a[, axis])

a 中移除长度为 1 的轴.

更改数组种类#

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

将输入转换为数组.

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

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

asmatrix (data[, dtype])

将输入解释为矩阵.

asfortranarray (a[, dtype, like])

返回一个在内存中按 Fortran 顺序排列的数组 (ndim >= 1).

ascontiguousarray (a[, dtype, like])

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

asarray_chkfinite (a[, dtype, order])

将输入转换为数组,检查 NaN 或 Inf.

require (a[, dtype, requirements, like])

返回满足要求的提供的类型的 ndarray.

连接数组#

concatenate ([axis, out, dtype, casting])

沿现有轴连接数组序列.

concat ([axis, out, dtype, casting])

沿现有轴连接数组序列.

stack (arrays[, axis, out, dtype, casting])

沿新轴连接数组序列.

block (arrays)

从块的嵌套列表中组装一个 nd-array.

vstack (tup, \[, dtype, casting])

按垂直方向(按行)顺序堆叠数组.

hstack (tup, \[, dtype, casting])

按水平方向(按列)顺序堆叠数组.

dstack (tup)

按深度方向(沿第三轴)顺序堆叠数组.

column_stack (tup)

将 1-D 数组作为列堆叠到 2-D 数组中.

分割数组#

split (ary, indices_or_sections[, axis])

将数组分割成多个子数组,作为对 ary 的视图.

array_split (ary, indices_or_sections[, axis])

将数组拆分为多个子数组.

dsplit (ary, indices_or_sections)

将数组沿第三个轴(深度)分割成多个子数组.

hsplit (ary, indices_or_sections)

将数组水平(按列)拆分为多个子数组.

vsplit (ary, indices_or_sections)

将数组垂直(按行)分割成多个子数组.

unstack (x, /, \[, axis])

沿给定轴将数组拆分为数组序列.

平铺数组#

tile (A, reps)

通过将 A 重复 reps 给定的次数来构造数组.

repeat (a, repeats[, axis])

在数组的每个元素之后重复该元素

添加和移除元素#

delete (arr, obj[, axis])

返回一个新的数组,其中的子数组沿轴被删除.

insert (arr, obj, values[, axis])

在给定索引之前,沿给定轴插入值.

append (arr, values[, axis])

将值追加到数组的末尾.

resize (a, new_shape)

返回具有指定形状的新数组.

trim_zeros (filt[, trim, axis])

删除沿一个维度但沿所有其他维度都为零的值.

unique (ar[, return_index, return_inverse, ...])

查找数组的唯一元素.

pad (array, pad_width[, mode])

填充数组.

重排元素#

flip (m[, axis])

沿给定轴反转数组中元素的顺序.

fliplr (m)

沿轴 1(左/右)反转元素的顺序.

flipud (m)

沿轴 0(上/下)反转元素的顺序.

roll (a, shift[, axis])

沿给定轴滚动数组元素.

rot90 (m[, k, axes])

在由轴指定的平面中将数组旋转 90 度.