小白教程
所有教程
关于
Search
172.69.58.68
172.69.58.68
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Numpy 数组操作”的源代码
本页内容
上一节:
NumPy_迭代数组
下一节:
NumPy_位运算
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Numpy 数组操作}}[[Category:NumPy 教程|13]] = Numpy 数组操作 = Numpy 中包含了一些函数用于处理数组,大概可分为以下几类: * [[#numpy_oparr1|修改数组形状]] * [[#numpy_oparr2|翻转数组]] * [[#numpy_oparr3|修改数组维度]] * [[#numpy_oparr4|连接数组]] * [[#numpy_oparr5|分割数组]] * [[#numpy_oparr6|数组元素的添加与删除]] == 修改数组形状 == {| class="table table-striped table-hover" ! 函数 ! 描述 |- | <code>reshape</code> | 不改变数据的条件下修改形状 |- | <code>flat</code> | 数组元素迭代器 |- | <code>flatten</code> | 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组 |- | <code>ravel</code> | 返回展开数组 |} === numpy.reshape === numpy.reshape 函数可以在不改变数据的条件下修改形状,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.reshape(arr, newshape, order='C') </sample> * <code>arr</code>:要修改形状的数组 * <code>newshape</code>:整数或者整数数组,新的形状应当兼容原有形状 * order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(8) print ('原始数组:') print (a) print ('\n') b = a.reshape(4,2) print ('修改后的数组:') print (b) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原始数组: [0 1 2 3 4 5 6 7] 修改后的数组: [[0 1] [2 3] [4 5] [6 7]] </sample> === numpy.ndarray.flat === numpy.ndarray.flat 是一个数组元素迭代器,实例如下: <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(9).reshape(3,3) print ('原始数组:') for row in a: print (row) #对数组中每个元素都进行处理,可以使用flat属性,该属性是一个数组元素迭代器: print ('迭代后的数组:') for element in a.flat: print (element) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原始数组: [0 1 2] [3 4 5] [6 7 8] 迭代后的数组: 0 1 2 3 4 5 6 7 8 </sample> === numpy.ndarray.flatten === numpy.ndarray.flatten 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组,格式如下: <sample title="" desc="" lang="python" hererun="1"> ndarray.flatten(order='C') </sample> 参数说明: * order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(8).reshape(2,4) print ('原数组:') print (a) print ('\n') # 默认按行 print ('展开的数组:') print (a.flatten()) print ('\n') print ('以 F 风格顺序展开的数组:') print (a.flatten(order = 'F')) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[0 1 2 3] [4 5 6 7]] 展开的数组: [0 1 2 3 4 5 6 7] 以 F 风格顺序展开的数组: [0 4 1 5 2 6 3 7] </sample> === numpy.ravel === numpy.ravel() 展平的数组元素,顺序通常是"C风格",返回的是数组视图(view,有点类似 C/C++引用reference的意味),修改会影响原始数组。 该函数接收两个参数: <sample title="" desc="" lang="python" hererun="1"> numpy.ravel(a, order='C') </sample> 参数说明: * order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(8).reshape(2,4) print ('原数组:') print (a) print ('\n') print ('调用 ravel 函数之后:') print (a.ravel()) print ('\n') print ('以 F 风格顺序调用 ravel 函数之后:') print (a.ravel(order = 'F')) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[0 1 2 3] [4 5 6 7]] 调用 ravel 函数之后: [0 1 2 3 4 5 6 7] 以 F 风格顺序调用 ravel 函数之后: [0 4 1 5 2 6 3 7] </sample> == 翻转数组 == {| class="table table-striped table-hover" ! 函数 ! 描述 |- | <code>transpose</code> | 对换数组的维度 |- | <code>ndarray.T</code> | 和 <code>self.transpose()</code> 相同 |- | <code>rollaxis</code> | 向后滚动指定的轴 |- | <code>swapaxes</code> | 对换数组的两个轴 |} === numpy.transpose === numpy.transpose 函数用于对换数组的维度,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.transpose(arr, axes) </sample> 参数说明: * <code>arr</code>:要操作的数组 * <code>axes</code>:整数列表,对应维度,通常所有维度都会对换。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(12).reshape(3,4) print ('原数组:') print (a ) print ('\n') print ('对换数组:') print (np.transpose(a)) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 对换数组: [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]] </sample> numpy.ndarray.T 类似 numpy.transpose: <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(12).reshape(3,4) print ('原数组:') print (a) print ('\n') print ('转置数组:') print (a.T) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 转置数组: [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]] </sample> === numpy.rollaxis === numpy.rollaxis 函数向后滚动特定的轴到一个特定位置,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.rollaxis(arr, axis, start) </sample> 参数说明: * <code>arr</code>:数组 * <code>axis</code>:要向后滚动的轴,其它轴的相对位置不会改变 * <code>start</code>:默认为零,表示完整的滚动。会滚动到特定位置。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np # 创建了三维的 ndarray a = np.arange(8).reshape(2,2,2) print ('原数组:') print (a) print ('获取数组中一个值:') print(np.where(a==6)) print(a[1,1,0]) # 为 6 print ('\n') # 将轴 2 滚动到轴 0(宽度到深度) print ('调用 rollaxis 函数:') b = np.rollaxis(a,2,0) print (b) # 查看元素 a[1,1,0],即 6 的坐标,变成 [0, 1, 1] # 最后一个 0 移动到最前面 print(np.where(b==6)) print ('\n') # 将轴 2 滚动到轴 1:(宽度到高度) print ('调用 rollaxis 函数:') c = np.rollaxis(a,2,1) print (c) # 查看元素 a[1,1,0],即 6 的坐标,变成 [1, 0, 1] # 最后的 0 和 它前面的 1 对换位置 print(np.where(c==6)) print ('\n') </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[[0 1] [2 3]] [[4 5] [6 7]]] 获取数组中一个值: (array([1]), array([1]), array([0])) 6 调用 rollaxis 函数: [[[0 2] [4 6]] [[1 3] [5 7]]] (array([0]), array([1]), array([1])) 调用 rollaxis 函数: [[[0 2] [1 3]] [[4 6] [5 7]]] (array([1]), array([0]), array([1])) </sample> === numpy.swapaxes === numpy.swapaxes 函数用于交换数组的两个轴,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.swapaxes(arr, axis1, axis2) </sample> * <code>arr</code>:输入的数组 * <code>axis1</code>:对应第一个轴的整数 * <code>axis2</code>:对应第二个轴的整数 <sample title="" desc="" lang="python" hererun="1"> import numpy as np # 创建了三维的 ndarray a = np.arange(8).reshape(2,2,2) print ('原数组:') print (a) print ('\n') # 现在交换轴 0(深度方向)到轴 2(宽度方向) print ('调用 swapaxes 函数后的数组:') print (np.swapaxes(a, 2, 0)) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[[0 1] [2 3]] [[4 5] [6 7]]] 调用 swapaxes 函数后的数组: [[[0 4] [2 6]] [[1 5] [3 7]]] </sample> == 修改数组维度 == {| class="table table-striped table-hover" ! 维度 ! 描述 |- | <code>broadcast</code> | 产生模仿广播的对象 |- | <code>broadcast_to</code> | 将数组广播到新形状 |- | <code>expand_dims</code> | 扩展数组的形状 |- | <code>squeeze</code> | 从数组的形状中删除一维条目 |} === numpy.broadcast === numpy.broadcast 用于模仿广播的对象,它返回一个对象,该对象封装了将一个数组广播到另一个数组的结果。 该函数使用两个数组作为输入参数,如下实例: <sample title="" desc="" lang="python" hererun="1"> import numpy as np x = np.array([[1], [2], [3]]) y = np.array([4, 5, 6]) # 对 y 广播 x b = np.broadcast(x,y) # 它拥有 iterator 属性,基于自身组件的迭代器元组 print ('对 y 广播 x:') r,c = b.iters # Python3.x 为 next(context) ,Python2.x 为 context.next() print (next(r), next(c)) print (next(r), next(c)) print ('\n') # shape 属性返回广播对象的形状 print ('广播对象的形状:') print (b.shape) print ('\n') # 手动使用 broadcast 将 x 与 y 相加 b = np.broadcast(x,y) c = np.empty(b.shape) print ('手动使用 broadcast 将 x 与 y 相加:') print (c.shape) print ('\n') c.flat = [u + v for (u,v) in b] print ('调用 flat 函数:') print (c) print ('\n') # 获得了和 NumPy 内建的广播支持相同的结果 print ('x 与 y 的和:') print (x + y) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 对 y 广播 x: 1 4 1 5 广播对象的形状: (3, 3) 手动使用 broadcast 将 x 与 y 相加: (3, 3) 调用 flat 函数: [[5. 6. 7.] [6. 7. 8.] [7. 8. 9.]] x 与 y 的和: [[5 6 7] [6 7 8] [7 8 9]] </sample> === numpy.broadcast_to === numpy.broadcast_to 函数将数组广播到新形状。它在原始数组上返回只读视图。 它通常不连续。 如果新形状不符合 NumPy 的广播规则,该函数可能会抛出ValueError。 <sample title="" desc="" lang="python" hererun="1"> numpy.broadcast_to(array, shape, subok) </sample> <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(4).reshape(1,4) print ('原数组:') print (a) print ('\n') print ('调用 broadcast_to 函数之后:') print (np.broadcast_to(a,(4,4))) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[0 1 2 3]] 调用 broadcast_to 函数之后: [[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]] </sample> === numpy.expand_dims === numpy.expand_dims 函数通过在指定位置插入新的轴来扩展数组形状,函数格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.expand_dims(arr, axis) </sample> 参数说明: * <code>arr</code>:输入数组 * <code>axis</code>:新轴插入的位置 <sample title="" desc="" lang="python" hererun="1"> import numpy as np x = np.array(([1,2],[3,4])) print ('数组 x:') print (x) print ('\n') y = np.expand_dims(x, axis = 0) print ('数组 y:') print (y) print ('\n') print ('数组 x 和 y 的形状:') print (x.shape, y.shape) print ('\n') # 在位置 1 插入轴 y = np.expand_dims(x, axis = 1) print ('在位置 1 插入轴之后的数组 y:') print (y) print ('\n') print ('x.ndim 和 y.ndim:') print (x.ndim,y.ndim) print ('\n') print ('x.shape 和 y.shape:') print (x.shape, y.shape) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 数组 x: [[1 2] [3 4]] 数组 y: [[[1 2] [3 4]]] 数组 x 和 y 的形状: (2, 2) (1, 2, 2) 在位置 1 插入轴之后的数组 y: [[[1 2]] [[3 4]]] x.ndim 和 y.ndim: 2 3 x.shape 和 y.shape: (2, 2) (2, 1, 2) </sample> === numpy.squeeze === numpy.squeeze 函数从给定数组的形状中删除一维的条目,函数格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.squeeze(arr, axis) </sample> 参数说明: * <code>arr</code>:输入数组 * <code>axis</code>:整数或整数元组,用于选择形状中一维条目的子集 <sample title="" desc="" lang="python" hererun="1"> import numpy as np x = np.arange(9).reshape(1,3,3) print ('数组 x:') print (x) print ('\n') y = np.squeeze(x) print ('数组 y:') print (y) print ('\n') print ('数组 x 和 y 的形状:') print (x.shape, y.shape) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 数组 x: [[[0 1 2] [3 4 5] [6 7 8]]] 数组 y: [[0 1 2] [3 4 5] [6 7 8]] 数组 x 和 y 的形状: (1, 3, 3) (3, 3) </sample> == 连接数组 == {| class="table table-striped table-hover" ! 函数 ! 描述 |- | <code>concatenate</code> | 连接沿现有轴的数组序列 |- | <code>stack</code> | 沿着新的轴加入一系列数组。 |- | <code>hstack</code> | 水平堆叠序列中的数组(列方向) |- | <code>vstack</code> | 竖直堆叠序列中的数组(行方向) |} === numpy.concatenate === numpy.concatenate 函数用于沿指定轴连接相同形状的两个或多个数组,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.concatenate((a1, a2, ...), axis) </sample> 参数说明: * <code>a1, a2, ...</code>:相同类型的数组 * <code>axis</code>:沿着它连接数组的轴,默认为 0 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2],[3,4]]) print ('第一个数组:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二个数组:') print (b) print ('\n') # 两个数组的维度相同 print ('沿轴 0 连接两个数组:') print (np.concatenate((a,b))) print ('\n') print ('沿轴 1 连接两个数组:') print (np.concatenate((a,b),axis = 1)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2] [3 4]] 第二个数组: [[5 6] [7 8]] 沿轴 0 连接两个数组: [[1 2] [3 4] [5 6] [7 8]] 沿轴 1 连接两个数组: [[1 2 5 6] [3 4 7 8]] </sample> === numpy.stack === numpy.stack 函数用于沿新轴连接数组序列,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.stack(arrays, axis) </sample> 参数说明: * <code>arrays</code>相同形状的数组序列 * <code>axis</code>:返回数组中的轴,输入数组沿着它来堆叠 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2],[3,4]]) print ('第一个数组:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二个数组:') print (b) print ('\n') print ('沿轴 0 堆叠两个数组:') print (np.stack((a,b),0)) print ('\n') print ('沿轴 1 堆叠两个数组:') print (np.stack((a,b),1)) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2] [3 4]] 第二个数组: [[5 6] [7 8]] 沿轴 0 堆叠两个数组: [[[1 2] [3 4]] [[5 6] [7 8]]] 沿轴 1 堆叠两个数组: [[[1 2] [5 6]] [[3 4] [7 8]]] </sample> === numpy.hstack === numpy.hstack 是 numpy.stack 函数的变体,它通过水平堆叠来生成数组。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2],[3,4]]) print ('第一个数组:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二个数组:') print (b) print ('\n') print ('水平堆叠:') c = np.hstack((a,b)) print (c) print ('\n') </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2] [3 4]] 第二个数组: [[5 6] [7 8]] 水平堆叠: [[1 2 5 6] [3 4 7 8]] </sample> === numpy.vstack === numpy.vstack 是 numpy.stack 函数的变体,它通过垂直堆叠来生成数组。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2],[3,4]]) print ('第一个数组:') print (a) print ('\n') b = np.array([[5,6],[7,8]]) print ('第二个数组:') print (b) print ('\n') print ('竖直堆叠:') c = np.vstack((a,b)) print (c) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2] [3 4]] 第二个数组: [[5 6] [7 8]] 竖直堆叠: [[1 2] [3 4] [5 6] [7 8]] </sample> == 分割数组 == {| class="table table-striped table-hover" ! 函数 ! 数组及操作 |- | <code>split</code> | 将一个数组分割为多个子数组 |- | <code>hsplit</code> | 将一个数组水平分割为多个子数组(按列) |- | <code>vsplit</code> | 将一个数组垂直分割为多个子数组(按行) |} === numpy.split === numpy.split 函数沿特定的轴将数组分割为子数组,格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.split(ary, indices_or_sections, axis) </sample> 参数说明: * <code>ary</code>:被分割的数组 * <code>indices_or_sections</code>:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭) * <code>axis</code>:设置沿着哪个方向进行切分,默认为 0,横向切分,即水平方向。为 1 时,纵向切分,即竖直方向。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(9) print ('第一个数组:') print (a) print ('\n') print ('将数组分为三个大小相等的子数组:') b = np.split(a,3) print (b) print ('\n') print ('将数组在一维数组中表明的位置分割:') b = np.split(a,[4,7]) print (b) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [0 1 2 3 4 5 6 7 8] 将数组分为三个大小相等的子数组: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])] 将数组在一维数组中表明的位置分割: [array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])] </sample> axis 为 0 时在水平方向分割,axis 为 1 时在垂直方向分割: <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(16).reshape(4, 4) print('第一个数组:') print(a) print('\n') print('默认分割(0轴):') b = np.split(a,2) print(b) print('\n') print('沿水平方向分割:') c = np.split(a,2,1) print(c) print('\n') print('沿水平方向分割:') d= np.hsplit(a,2) print(d) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 默认分割(0轴): [array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])] 沿水平方向分割: [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])] 沿水平方向分割: [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])] </sample> === numpy.hsplit === numpy.hsplit 函数用于水平分割数组,通过指定要返回的相同形状的数组数量来拆分原数组。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np harr = np.floor(10 * np.random.random((2, 6))) print ('原array:') print(harr) print ('拆分后:') print(np.hsplit(harr, 3)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 原array: [[4. 7. 6. 3. 2. 6.] [6. 3. 6. 7. 9. 7.]] 拆分后: [array([[4., 7.], [6., 3.]]), array([[6., 3.], [6., 7.]]), array([[2., 6.], [9., 7.]])] </sample> === numpy.vsplit === numpy.vsplit 沿着垂直轴分割,其分割方式与hsplit用法相同。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(16).reshape(4,4) print ('第一个数组:') print (a) print ('\n') print ('竖直分割:') b = np.vsplit(a,2) print (b) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 竖直分割: [array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])] </sample> == 数组元素的添加与删除 == {| class="table table-striped table-hover" ! 函数 ! 元素及描述 |- | <code>resize</code> | 返回指定形状的新数组 |- | <code>append</code> | 将值添加到数组末尾 |- | <code>insert</code> | 沿指定轴将值插入到指定下标之前 |- | <code>delete</code> | 删掉某个轴的子数组,并返回删除后的新数组 |- | <code>unique</code> | 查找数组内的唯一元素 |} === numpy.resize === numpy.resize 函数返回指定大小的新数组。 如果新数组大小大于原始大小,则包含原始数组中的元素的副本。 <sample title="" desc="" lang="python" hererun="1"> numpy.resize(arr, shape) </sample> 参数说明: * <code>arr</code>:要修改大小的数组 * <code>shape</code>:返回数组的新形状 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2,3],[4,5,6]]) print ('第一个数组:') print (a) print ('\n') print ('第一个数组的形状:') print (a.shape) print ('\n') b = np.resize(a, (3,2)) print ('第二个数组:') print (b) print ('\n') print ('第二个数组的形状:') print (b.shape) print ('\n') # 要注意 a 的第一行在 b 中重复出现,因为尺寸变大了 print ('修改第二个数组的大小:') b = np.resize(a,(3,3)) print (b) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2 3] [4 5 6]] 第一个数组的形状: (2, 3) 第二个数组: [[1 2] [3 4] [5 6]] 第二个数组的形状: (3, 2) 修改第二个数组的大小: [[1 2 3] [4 5 6] [1 2 3]] </sample> === numpy.append === numpy.append 函数在数组的末尾添加值。 追加操作会分配整个数组,并把原来的数组复制到新数组中。 此外,输入数组的维度必须匹配否则将生成ValueError。 append 函数返回的始终是一个一维数组。 <sample title="" desc="" lang="python" hererun="1"> numpy.append(arr, values, axis=None) </sample> 参数说明: * <code>arr</code>:输入数组 * <code>values</code>:要向<code>arr</code>添加的值,需要和<code>arr</code>形状相同(除了要添加的轴) * <code>axis</code>:默认为 None。当axis无定义时,是横向加成,返回总是为一维数组!当axis有定义的时候,分别为0和1的时候。当axis有定义的时候,分别为0和1的时候(列数要相同)。当axis为1时,数组是加在右边(行数要相同)。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2,3],[4,5,6]]) print ('第一个数组:') print (a) print ('\n') print ('向数组添加元素:') print (np.append(a, [7,8,9])) print ('\n') print ('沿轴 0 添加元素:') print (np.append(a, [[7,8,9]],axis = 0)) print ('\n') print ('沿轴 1 添加元素:') print (np.append(a, [[5,5,5],[7,8,9]],axis = 1)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2 3] [4 5 6]] 向数组添加元素: [1 2 3 4 5 6 7 8 9] 沿轴 0 添加元素: [[1 2 3] [4 5 6] [7 8 9]] 沿轴 1 添加元素: [[1 2 3 5 5 5] [4 5 6 7 8 9]] </sample> === numpy.insert === numpy.insert 函数在给定索引之前,沿给定轴在输入数组中插入值。 如果值的类型转换为要插入,则它与输入数组不同。 插入没有原地的,函数会返回一个新数组。 此外,如果未提供轴,则输入数组会被展开。 <sample title="" desc="" lang="python" hererun="1"> numpy.insert(arr, obj, values, axis) </sample> 参数说明: * <code>arr</code>:输入数组 * <code>obj</code>:在其之前插入值的索引 * <code>values</code>:要插入的值 * <code>axis</code>:沿着它插入的轴,如果未提供,则输入数组会被展开 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([[1,2],[3,4],[5,6]]) print ('第一个数组:') print (a) print ('\n') print ('未传递 Axis 参数。 在删除之前输入数组会被展开。') print (np.insert(a,3,[11,12])) print ('\n') print ('传递了 Axis 参数。 会广播值数组来配输入数组。') print ('沿轴 0 广播:') print (np.insert(a,1,[11],axis = 0)) print ('\n') print ('沿轴 1 广播:') print (np.insert(a,1,11,axis = 1)) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[1 2] [3 4] [5 6]] 未传递 Axis 参数。 在删除之前输入数组会被展开。 [ 1 2 3 11 12 4 5 6] 传递了 Axis 参数。 会广播值数组来配输入数组。 沿轴 0 广播: [[ 1 2] [11 11] [ 3 4] [ 5 6]] 沿轴 1 广播: [[ 1 11 2] [ 3 11 4] [ 5 11 6]] </sample> === numpy.delete === numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样,如果未提供轴参数,则输入数组将展开。 <sample title="" desc="" lang="python" hererun="1"> Numpy.delete(arr, obj, axis) </sample> 参数说明: * <code>arr</code>:输入数组 * <code>obj</code>:可以被切片,整数或者整数数组,表明要从输入数组删除的子数组 * <code>axis</code>:沿着它删除给定子数组的轴,如果未提供,则输入数组会被展开 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(12).reshape(3,4) print ('第一个数组:') print (a) print ('\n') print ('未传递 Axis 参数。 在插入之前输入数组会被展开。') print (np.delete(a,5)) print ('\n') print ('删除第二列:') print (np.delete(a,1,axis = 1)) print ('\n') print ('包含从数组中删除的替代值的切片:') a = np.array([1,2,3,4,5,6,7,8,9,10]) print (np.delete(a, np.s_[::2])) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 未传递 Axis 参数。 在插入之前输入数组会被展开。 [ 0 1 2 3 4 6 7 8 9 10 11] 删除第二列: [[ 0 2 3] [ 4 6 7] [ 8 10 11]] 包含从数组中删除的替代值的切片: [ 2 4 6 8 10] </sample> === numpy.unique === numpy.unique 函数用于去除数组中的重复元素。 <sample title="" desc="" lang="python" hererun="1"> numpy.unique(arr, return_index, return_inverse, return_counts) </sample> * <code>arr</code>:输入数组,如果不是一维数组则会展开 * <code>return_index</code>:如果为<code>true</code>,返回新列表元素在旧列表中的位置(下标),并以列表形式储 * <code>return_inverse</code>:如果为<code>true</code>,返回旧列表元素在新列表中的位置(下标),并以列表形式储 * <code>return_counts</code>:如果为<code>true</code>,返回去重数组中的元素在原数组中的出现次数 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.array([5,2,6,2,7,5,6,8,2,9]) print ('第一个数组:') print (a) print ('\n') print ('第一个数组的去重值:') u = np.unique(a) print (u) print ('\n') print ('去重数组的索引数组:') u,indices = np.unique(a, return_index = True) print (indices) print ('\n') print ('我们可以看到每个和原数组下标对应的数值:') print (a) print ('\n') print ('去重数组的下标:') u,indices = np.unique(a,return_inverse = True) print (u) print ('\n') print ('下标为:') print (indices) print ('\n') print ('使用下标重构原数组:') print (u[indices]) print ('\n') print ('返回去重元素的重复数量:') u,indices = np.unique(a,return_counts = True) print (u) print (indices) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 第一个数组: [5 2 6 2 7 5 6 8 2 9] 第一个数组的去重值: [2 5 6 7 8 9] 去重数组的索引数组: [1 0 2 4 7 9] 我们可以看到每个和原数组下标对应的数值: [5 2 6 2 7 5 6 8 2 9] 去重数组的下标: [2 5 6 7 8 9] 下标为: [1 0 2 0 3 1 2 4 0 5] 使用下标重构原数组: [5 2 6 2 7 5 6 8 2 9] 返回去重元素的重复数量: [2 5 6 7 8 9] [3 2 2 1 1 1] </sample>
返回至“
Numpy 数组操作
”。
上一节:
NumPy_迭代数组
下一节:
NumPy_位运算