数组操作例程#

基本操作#

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 (数组)

从嵌套的块列表中组装一个 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)

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

hsplit (ary, indices_or_sections)

将数组水平(按列)分割成多个子数组.

vsplit (ary, indices_or_sections)

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

unstack (x, /, [, axis])

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

平铺数组#

tile (A, reps)

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

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 度.