数组操作例程
该文章主要介绍基本操作、更改数组形状、类似转置操作、更改维数、更改数组类型、连接数组、拆分数组、平铺数组、添加和删除元素以及重新排列元素以访问数据和子数组的功能,以及分割、重塑和连接数组。接下来sojon给大家继续讲解。
基本操作 |
方法名 | 描述 | 句法 |
copyto()
| 返回给定形状和类型的新数组,而不初始化条目。 | empty(shape[, dtype, order]) |
改变阵列形状 |
姓名 | 描述 | 句法 |
reshape()
| 为数组提供新的形状而不更改其数据。 | reshape(a, newshape[, order]) |
ravel()
| 返回连续的展平数组。 | ravel(a[, order]) |
ndarray.flat | 数组上的一维迭代器。 | ndarray.flat |
ndarray.flatten() | 返回折叠成一维的数组的副本。 | ndarray.flatten([order]) |
类似转置的操作 |
方法名
| 描述 | 句法 |
moveaxis()
| 将数组的轴移动到新位置。 | moveaxis(a, source, destination) |
rollaxis
| 向后滚动指定轴,直到其位于给定位置。 | rollaxis(a, axis[, start]) |
swapaxes()
| 交换数组的两个轴。 | swapaxes(a, axis1, axis2) |
ndarray.T | 与 self.transpose() 相同,但如果 self.ndim < 2 则返回 self。 | |
transpose()
| 返回与给定数组具有相同形状和类型的完整数组。 | full_like(a, fill_value[, dtype, order, subok]) |
更改维度数 |
方法名
| 描述 | 句法 |
atleast_1d()
| 将输入转换为至少具有一维的数组。 | atleast_1d(*arys) |
atleast_2d()
| 将输入视为至少具有二维的数组。 | atleast_2d(*arys) |
atleast_3d()
| 将输入视为至少具有三个维度的数组。 | atleast_3d(*arys) |
broadcast
| 生成一个模仿广播的对象。 | broadcast |
broadcast_to()
| 将数组广播为新形状。 | broadcast_to(array, shape[, subok]) |
broadcast_arrays()
| 相互广播任意数量的数组。 | broadcast_arrays(*args, **kwargs) |
expand_dims()
| 扩展数组的形状。 | expand_dims(a, axis) |
squeeze()
| 从数组形状中删除一维条目。 | squeeze(a[, axis]) |
改变数组类型 |
方法名
| 描述 | 句法 |
asarray() | 将输入转换为数组。 | asarray(a[, dtype, order]) |
asanyarray() | 将输入转换为 ndarray,但传递 ndarray 子类。 | asanyarray(a[, dtype, order]) |
asmatrix()
| 将输入解释为矩阵。 | asmatrix(data[, dtype]) |
asfarray() | 返回转换为浮点类型的数组。 | asfarray(a[, dtype]) |
asfortranarray() | 返回内存中按 Fortran 顺序排列的数组。 | asfortranarray(a[, dtype]) |
ascontiguousarray()
| 返回内存中的连续数组(C 顺序)。 | ascontiguousarray(a[, dtype]) |
asarray_chkfinite() | 将输入转换为数组,检查 NaN 或 Infs。 | asarray_chkfinite(a[, dtype, order]) |
asscalar
| 将大小为 1 的数组转换为其等价标量。 | asscalar(a) |
require
| 返回满足要求的所提供类型的 ndarray。 | require(a[, dtype, requirements]) |
连接数组 |
方法名
| 描述 | 句法 |
concatenate()
| 沿现有轴连接一系列数组。 | concatenate((a1, a2, …)[, axis, out]) |
stack()
| 沿新轴连接一系列数组。 | stack(arrays[, axis, out]) |
column_stack()
| 将一维数组作为列堆叠到二维数组中。 | column_stack(tup) |
dstack() | 按深度顺序(沿第三轴)堆叠数组。 | dstack(tup) |
hstack() | 按水平顺序堆叠数组(按列)。 | hstack(tup) |
vstack()
| 垂直(按行)顺序堆叠数组。 | vstack(tup) |
block()
| 从嵌套的块列表组装一个 nd 数组。 | block(arrays)) |
分割数组 |
方法名
| 描述 | 句法 |
split()
| 将一个数组拆分为多个子数组。 | split(ary,indices_or_sections [,axis]) |
array_split()
| 将一个数组拆分为多个子数组。 | array_split(ary,indices_or_sections [,axis]) |
dsplit() | 沿第三轴(深度)将数组拆分为多个子数组。 | dsplit(ary,indices_or_sections) |
hsplit() | 将数组水平(按列)拆分为多个子数组。 | hsplit(ary,indices_or_sections) |
vsplit() | 将数组垂直(按行)拆分为多个子数组。 | vsplit(ary,indices_or_sections) |
平铺数组 |
方法名
| 描述 | 句法 |
tile()
| 通过重复 A 指定次数来构建数组。 | tile(A, reps) |
repeat()
| 重复数组的元素。 | repeat(a, repeats[, axis]) |
添加和删除元素 |
方法名
| 描述 | 句法 |
delete()
| 返回一个新数组,其中沿轴删除了子数组。 | delete(arr, obj[, axis]) |
insert()
| 沿给定轴在给定索引之前插入值。 | insert(arr, obj, values[, axis]) |
append()
| 将值附加到数组末尾。 | append(arr, values[, axis]) |
resize()
| 返回具有指定形状的新数组。 | resize(a, new_shape) |
trim_zeros()
| 修剪一维数组或序列中的前导和/或尾随零。 | trim_zeros(filt[, trim]) |
unique()
| 找出数组中唯一的元素。 | unique(ar[, return_index, return_inverse, …]) |
重新排列元素 |
方法名
| 描述 | 句法 |
flip()
| 沿给定轴反转数组中元素的顺序。 | flip(m[, axis]) |
fliplr()
| 向左/右方向翻转数组。 | fliplr(m) |
flipud()
| 向上/向下翻转阵列。 | flipud(m) |
reshape()
| 为数组提供新的形状而不更改其数据。 | reshape(a, newshape[, order]) |
roll()
| 沿给定轴滚动数组元素。 | roll(a, shift[, axis]) |
rot90() | 在轴指定的平面中将数组旋转 90 度。 | rot90(m[, k, axes])) |
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/513.html
转载时必须以链接形式注明原始出处及本声明。
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。