数组操作例程#

基本操作#

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

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

ndim(a)

返回数组的维数。

shape(a)

返回数组的形状。

size(a[, axis])

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

改变数组形状#

reshape(a, /, shape[, order, 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(*arrays)

生成一个模拟广播的对象。

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 顺序布局的数组(维数 >= 1)。

ascontiguousarray(a[, dtype, like])

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

asarray_chkfinite(a[, dtype, order])

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

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

返回一个符合要求的指定类型的 ndarray。

连接数组#

concatenate(arrays, /[, axis, out, dtype, ...])

沿现有轴连接数组序列。

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

沿现有轴连接数组序列。

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

沿新轴连接数组序列。

block(arrays)

从嵌套块列表中组装 ndarray。

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

按顺序垂直(逐行)堆叠数组。

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

按顺序水平(逐列)堆叠数组。

dstack(tup)

按顺序按深度(沿第三轴)堆叠数组。

column_stack(tup)

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

分割数组#

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)

通过重复 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])

在由 axes 指定的平面中将数组旋转 90 度。