数组操作例程#

基本操作#

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

将值从一个数组复制到另一个数组,并根据需要进行广播。

ndim(a)

返回数组的维度数量。

shape(a)

返回数组的形状。

size(a[, axis])

返回沿给定轴的元素数量。

改变数组形状#

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

赋予数组新的形状而不改变其数据。

ravel(a[, order])

返回一个连续的扁平数组。

ndarray.flat

数组上的一维迭代器。

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 中移除长度为一的轴。

改变数组类型#

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

返回一个在内存中连续的数组 (ndim >= 1)(C 顺序)。

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)

将一维数组作为列堆叠成二维数组。

分割数组#

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 指定次数来构造数组。

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