Lesson6——NumPy 数组操作

NumPy 教程目录

1 基础操作

1.1 numpy.shape(a)

  • numpy.shape(a)
  • 返回数组的形状。

Examples:

import numpy as np
np.shape(np.ones([3,2]))
(3, 2)

np.shape([1,2])
(2,)

np.shape([[1,2]])
(1, 2)

np.shape(0)
()

np.shape([0])
(1,)

a = np.array([(1, 2), (3, 4), (5, 6)],
dtype=[('x', 'i4'), ('y', 'i4')])
np.shape(a)
(3,)

2 改变数组形状

2.1 numpy.reshape

  • numpy.reshape(a, newshape, order='C')
  • order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'k' -- 元素在内存中的出现顺序。
  • 在不更改其数据的情况下为数组赋予新形状。

Examples:

a = np.zeros((3,2))
np.reshape(a,(2,3))
[[0., 0., 0.],
[0., 0., 0.]]

np.reshape(a,6)
[0., 0., 0., 0., 0., 0.]

np.reshape(np.arange(6).reshape(2,3),6,order='F')
[0, 3, 1, 4, 2, 5]

np.reshape(np.arange(6).reshape(2,3),6,order='C')
[0, 1, 2, 3, 4, 5]

np.reshape(np.arange(6),(3,-1),order='C')
array([[0, 1],
[2, 3],
[4, 5]])

2.2 numpy.ravel

  • numpy.ravel(a, order='C')
  • 返回一个连续的展开数组。

Examples:

x = np.array([[1,2,3],[4,5,6]])
x
[[1, 2, 3],
       [4, 5, 6]]

np.ravel(x)
[1, 2, 3, 4, 5, 6]

x.reshape(-1)
[1, 2, 3, 4, 5, 6]

np.ravel(x,order='F')
[1, 4, 2, 5, 3, 6]

Examples:

np.ravel(x.T)
[1, 4, 2, 5, 3, 6]

x.T
[[1 4]
[2 5]
[3 6]]

np.ravel(x.T,order = "A")
[1, 2, 3, 4, 5, 6]

x.T
[[1 4]
[2 5]
[3 6]]

np.ravel(x.T,order = "C")
1, 4, 2, 5, 3, 6]

x.T
[[1 4]
[2 5]
[3 6]]

np.ravel(x.T,order = "K")
[1, 2, 3, 4, 5, 6]

Examples:

a = np.arange(12).reshape(2,3,2).swapaxes(1,2)
a
[[[ 0 2 4]
[ 1 3 5]]
[[ 6 8 10]
[ 7 9 11]]]

a.ravel(order='C')
[ 0, 2, 4, 1, 3, 5, 6, 8, 10, 7, 9, 11]

a.ravel(order='F')
[ 0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11]

a.ravel(order='K')
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

2.3 numpy.ndarray.flat

  • ndarray.flat
  • 一个数组元素迭代器。
  • 属性

Examples

x  = np.arange(1,7).reshape(2,3)
x
#结果
[[1, 2, 3],
 [4, 5, 6]]

x.flat[3]
#结果
4

x.T
#结果
[[1, 4],
 [2, 5],
 [3, 6]]

for i in x.T.flat:
    print(i,end=' ')
#结果
1 4 2 5 3 6 

x.flat[[2,3]]
#结果
[3, 4]

x.flat[[3,4]]
#结果
[4, 5]

2.4 numpy.ndarray.flatten

  • ndarray.flatten(order='C')
  • order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。
  • 返回折叠成一维的数组的拷贝。
  • 方法

Examples

x = np.array([[1,2],[3,4]])
x
#输出结果
[[1, 2],
 [3, 4]]

x.flatten()
#输出结果
[1, 2, 3, 4]

x.flatten('F')
#输出结果
[1, 3, 2, 4]

x.flatten('K')
#输出结果
[1, 2, 3, 4]

3 类似转置的操作

3.1 numpy.moveaxis

  • numpy.moveaxis(a, source, destination)
  • 将数组的轴移动到新位置。

Examples

x = np.zeros((3,4,5))
np.moveaxis(x,0,-1).shape #source 在 0 轴,destination在 2 轴,移动 3 到最后面。
#输出结果
(4, 5, 3)

np.moveaxis(x,-1,0).shape
#输出结果
(5, 3, 4)

np.transpose(x).shape
#输出结果
(5, 4, 3)

np.swapaxes(x,0,-1).shape
#输出结果
(5, 4, 3)

np.moveaxis(x,[0,1],[-1,-2]).shape
#输出结果
(5, 4, 3)

np.moveaxis(x,[0,1,2],[-1,-2,-3]).shape
#输出结果
(5, 4, 3)

3.2 numpy.rollaxis

  • numpy.rollaxis(a, axis, start=0)
  • 向后滚动指定的轴,直到它位于给定位置。[ 可以理解为 将 axis 提出,插入到 start 轴 ]

Examples:

a = np.ones((3,4,5,6))
print(np.rollaxis(a,3,1).shape) #将 3 轴的 6 滚动到 1 轴。
(3, 6, 4, 5)

print(np.rollaxis(a,2).shape)
(5, 3, 4, 6)

print(np.rollaxis(a,1,4).shape)
(3, 5, 6, 4)

3.3 numpy.swapaxes

  • numpy.swapaxes(a, axis1, axis2)
  • 交换数组的两个轴。

Examples:

x = np.array([[1,2,3]]) #shape=[1,3]
np.swapaxes(x,0,1) #shape=[3,1]
[[1],
 [2],
 [3]]

x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) #shape = [2,2,2]
x
[[[0, 1],
 [2, 3]],
 [[4, 5],
 [6, 7]]] 

np.swapaxes(x,0,2)
[[[0, 4],
 [2, 6]],
 [[1, 5],
 [3, 7]]]

Examples:

x = np.arange(24).reshape((2,3,4))
x.shape
(2, 3, 4)

np.swapaxes(x,0,0)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

np.swapaxes(x,0,0).shape
(2, 3, 4)

np.swapaxes(x,0,1)
[[[ 0  1  2  3]
  [12 13 14 15]]

 [[ 4  5  6  7]
  [16 17 18 19]]

 [[ 8  9 10 11]
  [20 21 22 23]]]

np.swapaxes(x,0,1).shape
(3, 2, 4)

3.4 numpy.ndarray.T

  • ndarray.T
  • 转置数组。
  • attribute

Examples:

x = np.array([[1.,2.],[3.,4.]])
x
array([[ 1.,  2.],
       [ 3.,  4.]])

x.T
array([[ 1.,  3.],
       [ 2.,  4.]])

x = np.array([1.,2.,3.,4.])
x
array([ 1.,  2.,  3.,  4.])

x.T
array([ 1.,  2.,  3.,  4.])

3.5 numpy.transpose

  • numpy.transpose(a, axes=None)
  • 反转或置换数组的轴; 返回修改后的数组。

Examples:

x = np.arange(6).reshape((2,3))
np.transpose(x).shape
(3, 2)

x = np.arange(6).reshape((1,2,3)) #x.shape = [1,2,3]
np.transpose(x).shape  #相当于转置
(3, 2, 1)

x = np.arange(6).reshape((1,2,3))
np.transpose(x,(1,0,2)).shape  #相当于将 1 轴替换到 0 轴,将 0 轴替换到 1 轴,将 2轴替换到 2 轴
(2, 1, 3)

np.transpose(x,(1,2,0)).shape#相当于将 1 轴替换到 0 轴,将 2 轴替换到 1 轴,将 0 轴替换到 2 轴
(2, 3, 1)

Examples: 研究交换轴,查找原来元素的位置。

x=np.arange(16).reshape((2,2,4)) 
print(x)
print(x[0,1,3])  #此时数组中的 7 的坐标是(0,1,3)
"""
[[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]]
  
7
"""

y = np.transpose(x,(2,1,0)) #将 2 轴代替 0 轴,1 轴不变,0 轴代替 2 轴
print(y)
print(y.shape)
print(y[3,1,0]) 那么数组中的 7 的坐标由(0,1,3) 变为 (3,1,0)。[其实就是将(2,1,0)看出索引,只需要将对应的值替换过来就行]

"""
[[[ 0  8]
  [ 4 12]]

 [[ 1  9]
  [ 5 13]]

 [[ 2 10]
  [ 6 14]]

 [[ 3 11]
  [ 7 15]]]
  
(4, 2, 2)

7
"""

z = np.transpose(x,(1,2,0))
print(z)
print(z.shape)
print(z[1,3,0])
"""
[[[ 0  8]
  [ 1  9]
  [ 2 10]
  [ 3 11]]

 [[ 4 12]
  [ 5 13]
  [ 6 14]
  [ 7 15]]]
  
(2, 4, 2)

7
"""

4 修改数组维度

4.1 numpy.expand_dims

  • numpy.expand_dims(a, axis)
  • 扩展数组的形状。通过在指定位置插入新的轴来扩展数组形状。

Examples:

x = np.arange(2)
print(x)
print(x.shape)
print(np.expand_dims(x,axis=0).shape)
print(np.expand_dims(x,axis=1).shape)
print(np.expand_dims(x,axis= (0,1)).shape)
"""
[0 1]

(2,)

(1, 2)

(2, 1)

(1, 1, 2)
"""

4.2 numpy.squeeze

  • numpy.squeeze(a, axis=None)
  • 从给定数组 a 的形状中删除一维的条目

Examples:

x = np.arange(6).reshape((1,2,3))
print(x)
print(x.shape)
print(np.squeeze(x).shape)
"""
[[[0 1 2]
  [3 4 5]]]
  
(1, 2, 3)

(2, 3)

(2, 3)
"""

x = np.arange(6).reshape((1,1,2,3))
print(x)
print(x.shape)
print(np.squeeze(x).shape)
"""
[[[[0 1 2]
   [3 4 5]]]]
   
(1, 1, 2, 3)

(2, 3)
"""

Examples:

x = np.arange(6).reshape((1,2,3))
print(x)
print(x.shape)
print(np.squeeze(x,axis=0).shape)
#print(np.squeeze(x,axis=1).shape)
"""
[[[0 1 2]
  [3 4 5]]]
  
(1, 2, 3)

(2, 3)
"""

x = np.array([[123]])
print(x)
print(x.shape)
print(np.squeeze(x))
print(np.squeeze(x)[()])
"""
[[123]]

(1, 1)

123

123
"""

5 连接数组

5.1 numpy.concatenate

  • numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
  • 连接沿现有轴的数组序列

Examples:

a = np.arange(4).reshape((2,2))
print('a = ',a)
print('a.shape = ',a.shape)
"""
a =  [[0 1]
 [2 3]]
 
a.shape =  (2, 2)
"""

b = np.arange(4,6).reshape((1,2))
print('b = ',b)
print('b.shape = ',b.shape)
"""
b =  [[4 5]]

b.shape =  (1, 2)
"""

c = np.concatenate((a,b),axis=0)  #上面 a.shape = [2,2] ,b.shape = [1,2] ,由于对 0  轴拼接,只需要将 0 轴维度相加即可 
print('c = ',c)
print('c.shape = ',c.shape)
"""
c =  [[0 1]
 [2 3]
 [4 5]]
 
 c.shape =  (3, 2)
"""

d = np.concatenate((a,b),axis=None)
print('d = ',d)
print('d.shape = ',d.shape)
"""
d =  [0 1 2 3 4 5]

d.shape =  (6,)
"""

5.2 numpy.stack

  • numpy.stack(arrays, axis=0, out=None)
  • 沿着新的轴加入一系列数组。

Examples:

x = np.arange(6).reshape((2,3))
print(x)
"""
[[0 1 2]
 [3 4 5]]
"""

print(np.stack(x,axis =0))
print(np.stack(x,axis =0).shape)
"""
[[0 1 2]
 [3 4 5]]
 
(2, 3)
"""

print(np.stack(x,axis =1))
print(np.stack(x,axis =1).shape)
"""
[[0 3]
 [1 4]
 [2 5]]
 
(3, 2)
"""

print(np.stack(x,axis =-1))
print(np.stack(x,axis =-1).shape)
"""
[[0 3]
 [1 4]
 [2 5]]
 
(3, 2)
"""

Examples:

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),axis =0))
print ('\n')
 
print ('沿轴 1 堆叠两个数组:')
print (np.stack((a,b),axis =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]]]

"""

5.3 numpy.hstack

  • numpy.hstack(tup)
  • numpy.hstack 是 numpy.stack 函数的变体,它通过水平堆叠来生成数组

Examples:

a = np.array((1,2,3))
b = np.array((4,5,6))
np.hstack((a,b))
"""
array([1, 2, 3, 4, 5, 6])
"""

a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
print(a.shape)
print(b.shape)
np.hstack((a,b))
"""
(3, 1)

(3, 1)

array([[1, 4],
       [2, 5],
       [3, 6]])
"""

Examples:

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

"""
第一个数组:
[[1 2]
 [3 4]]

第二个数组:
[[5 6]
 [7 8]]

水平堆叠:
[[1 2 5 6]
 [3 4 7 8]]

"""

5.4 numpy.vstack

  • numpy.vstack(tup)
  • numpy.vstack 是 numpy.stack 函数的变体,它通过垂直堆叠来生成数组

Examples:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.vstack((a,b))
"""
array([[1, 2, 3],
       [4, 5, 6]])
"""

a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[6]])
print(a.shape)
print(b.shape)
np.vstack((a,b))

"""
(3, 1)
(3, 1)
array([[1],
       [2],
       [3],
       [5],
       [6]])
"""

Examples:

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)
"""
第一个数组:
[[1 2]
 [3 4]]

第二个数组:
[[5 6]
 [7 8]]

竖直堆叠:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
"""

5.5 numpy.dstack

  • numpy.dstack(tup)
  • 按顺序深度(沿第三轴)堆叠数组。

Examples:

a = np.array((1,2,3))
b = np.array((2,3,4))
np.dstack((a,b))
"""
array([[[1, 2],
        [2, 3],
        [3, 4]]])
"""

a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.dstack((a,b))
"""
array([[[1, 2]],
       [[2, 3]],
"""

5.6 numpy.column_stack

  • numpy.column_stack(tup)
  • 将一维数组作为列堆叠到二维数组中。

Examples:

a = np.array((1,2,3))
b = np.array((2,3,4))
np.column_stack((a,b))
"""
array([[1, 2],
       [2, 3],
       [3, 4]])
"""

5.7 numpy.row_stack

  • numpy.row_stack(tup)
  • 垂直(按行)按顺序堆叠数组。

Examples:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.vstack((a,b))
"""
array([[1, 2, 3],
       [4, 5, 6]])
"""

a = np.array([[1], [2], [3]])
b = np.array([[4], [5], [6]])
np.vstack((a,b))
"""
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])
"""

6 拆分数组

6.1 numpy.split

  • numpy.split(ary, indices_or_sections, axis=0)
  • 沿特定的轴将数组分割为子数组。
  • 参数说明:
    • ary:被分割的数组
    • indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
    • axis:设置沿着哪个方向进行切分,默认为 0,横向切分,即水平方向。为 1 时,纵向切分,即竖直方向。

Examples:

x = np.arange(8)
print(np.split(x,2))
print(np.split(x,[2,4]))
"""
[array([0, 1, 2, 3]), array([4, 5, 6, 7])]
[array([0, 1]), array([2, 3]), array([4, 5, 6, 7])]
"""

Examples:

a = np.arange(9)
 
print ('第一个数组:')
print (a)
 
print ('将数组分为三个大小相等的子数组:')
b = np.split(a,3)
print (b)
 
print ('将数组在一维数组中表明的位置分割:')
b = np.split(a,[4,7])
print (b)

"""
第一个数组:
[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])]

"""

Examples:

a = np.arange(16).reshape(4, 4)
print('第一个数组:')
print(a)

print('默认分割(0轴):')
b = np.split(a,2)
print(b)

print('沿垂直方向分割:')
c = np.split(a,2,axis=1)
print(c)

print('沿水平方向分割:')
d= np.hsplit(a,2)
print(d)

"""
第一个数组:
[[ 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]])]
"""

6.2 numpy.array_split

  • numpy.array_split(ary, indices_or_sections, axis=0)
  • 将一个数组拆分为多个子数组。

Examples:

x = np.arange(8)
print(np.array_split(x,2))
print(np.array_split(x,[2,4]))
"""
[array([0, 1, 2, 3]), array([4, 5, 6, 7])]
[array([0, 1]), array([2, 3]), array([4, 5, 6, 7])]
"""

x = np.arange(9)
print(np.array_split(x,2))
print(np.array_split(x,[2,4]))
"""
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8])]
[array([0, 1]), array([2, 3]), array([4, 5, 6, 7, 8])]
"""

6.3 numpy.dsplit

  • numpy.dsplit(ary, indices_or_sections)
  • 沿第 3 轴(深度)将数组拆分为多个子数组。

Examples:

x = np.arange(16.0).reshape(2, 2, 4)
print(x)
"""
[[[ 0.  1.  2.  3.]
  [ 4.  5.  6.  7.]]

 [[ 8.  9. 10. 11.]
  [12. 13. 14. 15.]]]
"""

print(np.dsplit(x,2))
"""
[array([[[ 0.,  1.],
        [ 4.,  5.]],

       [[ 8.,  9.],
        [12., 13.]]]), array([[[ 2.,  3.],
        [ 6.,  7.]],

       [[10., 11.],
        [14., 15.]]])]
"""

print(np.dsplit(x,np.array([3, 6])))
#输出结果
"""
[array([[[ 0.,  1.,  2.],
        [ 4.,  5.,  6.]],

       [[ 8.,  9., 10.],
        [12., 13., 14.]]]), array([[[ 3.],
        [ 7.]],

       [[11.],
        [15.]]]), array([], shape=(2, 2, 0), dtype=float64)]
"""

6.4 numpy.hsplit

  • numpy.hsplit(ary, indices_or_sections)
  • 用于水平分割数组,通过指定要返回的相同形状的数组数量来拆分原数组。

Examples:

x = np.arange(16.0).reshape(4, 4)
print(x)
"""
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]
 [12. 13. 14. 15.]]
"""

np.hsplit(x, 2)
"""
[array([[ 0.,  1.],
        [ 4.,  5.],
        [ 8.,  9.],
        [12., 13.]]),
 array([[ 2.,  3.],
        [ 6.,  7.],
        [10., 11.],
        [14., 15.]])]
"""

np.hsplit(x, np.array([3, 6]))
"""
[array([[ 0.,  1.,  2.],
        [ 4.,  5.,  6.],
        [ 8.,  9., 10.],
        [12., 13., 14.]]),
 array([[ 3.],
        [ 7.],
        [11.],
        [15.]]),
 array([], shape=(4, 0), dtype=float64)]
"""

x = np.arange(8.0).reshape(2, 2, 2)
np.hsplit(x, 2)
"""
[array([[ 0.,  1.,  2.],
        [ 4.,  5.,  6.],
        [ 8.,  9., 10.],
        [12., 13., 14.]]),
 array([[ 3.],
        [ 7.],
        [11.],
        [15.]]),
 array([], shape=(4, 0), dtype=float64)]

""""
[array([[[0., 1.]],
 
        [[4., 5.]]]),
 array([[[2., 3.]],
 
        [[6., 7.]]])]
"""

Examples:

harr = np.floor(10 * np.random.random((2, 6)))
print ('原array:')
print(harr)
 
print ('拆分后:')
print(np.hsplit(harr, 3))

"""
原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.]])]
"""

6.5 numpy.vsplit

  • numpy.vsplit(ary, indices_or_sections)
  • 将一个数组垂直拆分为多个子数组(逐行)。

Examples:

x = np.arange(16.0).reshape(4, 4)
x
#输出结果
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  6.,  7.],
       [ 8.,  9., 10., 11.],
       [12., 13., 14., 15.]])

np.vsplit(x, 2)
"""
[array([[0., 1., 2., 3.],
        [4., 5., 6., 7.]]),
 array([[ 8.,  9., 10., 11.],
        [12., 13., 14., 15.]])]
"""

np.vsplit(x, np.array([3, 6]))
"""
[array([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]]),
 array([[12., 13., 14., 15.]]),
 array([], shape=(0, 4), dtype=float64)]
"""

x = np.arange(8.0).reshape(2, 2, 2)
np.vsplit(x, 2)
"""
[array([[[0., 1.],
         [2., 3.]]]),
 array([[[4., 5.],
         [6., 7.]]])]
"""

Examples:

a = np.arange(16).reshape(4,4)
 
print ('第一个数组:')
print (a)
print ('\n')
 
print ('竖直分割:')
b = np.vsplit(a,2)
print (b)

"""
第一个数组:
[[ 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]])]
"""

7 平铺阵列

7.1 numpy.tile

  • numpy.tile(A, reps)
  • 通过重复 Areps 给出的次数来构造一个数组。

Examples:

x =np.array([0,1,2])
print(np.tile(x,2))
"""
[0 1 2 0 1 2]
"""

x =np.array([0,1,2])
print(np.tile(x,(2,2)))
"""
[[0 1 2 0 1 2]
 [0 1 2 0 1 2]]
"""

x =np.array([0,1,2])
print(np.tile(x,(2,1,2)))
"""
[[[0 1 2 0 1 2]]
 [[0 1 2 0 1 2]]]
"""

b = np.array([[1, 2], [3, 4]])
print(np.tile(b, 2))
"""
[[1 2 1 2]
 [3 4 3 4]]
"""

print(np.tile(b, (2,1)))
"""
[[1 2]
 [3 4]
 [1 2]
 [3 4]]
"""

c = np.array([1,2,3,4])
print(np.tile(c,(4,1)))
"""
[[1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]
 [1 2 3 4]]
"""

7.2 numpy.repeat

  • numpy.repeat(a, repeats, axis=None)
  • 重复数组的元素。

Examples:

print(np.repeat(3, 4))
"""
array([3, 3, 3, 3])
"""

x = np.array([[1,2],[3,4]])
print(np.repeat(x, 2))
"""
array([1, 1, 2, 2, 3, 3, 4, 4])
"""

print(np.repeat(x, [1, 2], axis=0))
"""
array([[1, 2],
       [3, 4],
       [3, 4]])
"""

print(np.repeat(x, 3, axis=1))
"""
array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]])
"""

8 添加和删除元素

8.1 numpy.delete

  • numpy.delete(arr, obj, axis=None)
  • 返回一个新数组,其中删除了沿轴的子数组。 对于一维数组,这将返回 arr[obj] 未返回的那些条目。

Examples:

arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr)
"""
[[1 2 3]
 [4 5 6]
 [7 8 9]]
"""

print(np.delete(arr,[1,2],axis=0)) #删除 0 轴中的1、2行,即将[4,5,6],[7,8,9] 删除了
"""
[[1 2 3]]
"""

print(np.delete(arr,[1,2],axis = 1)) #删除 1 轴中的 1、2列 ,即将每行第[1,2]列删除了
"""
[[1]
 [4]
 [7]]
"""

print(np.delete(arr,1,axis = None))
"""
[1 3 4 5 6 7 8 9]
"""

print(np.delete(arr,[1,2],axis = None)) #将索引为 1、2 的元素删除
"""
[1 4 5 6 7 8 9]
"""

  reference

8.2 numpy.insert

  • numpy.insert(arr, obj, values, axis=None)
  • 在给定索引之前沿给定轴插入值。
  • 参数说明:
    • arr:输入数组
    • obj:在其之前插入值的索引
    • values:要插入的值
    • axis:沿着它插入的轴,如果未提供,则输入数组会被展开

Examples:

a = np.array([[1,2],[3,4],[5,6]])
print(a)
"""
[[1 2]
 [3 4]
 [5 6]]
"""

print(np.insert(a,1,5)) #在位置 1 插入 5。
"""
[1 5 2 3 4 5 6]
"""


print(np.insert(a,1,5,axis =0)) #0轴下,在位置 1 插入 [5,5]。
"""
[[1 2]
 [5 5]
 [3 4]
 [5 6]]
"""

print(np.insert(a,1,5,axis =1))#1轴下,在位置 1 插入 [5,5,5]。
"""
[[1 5 2]
 [3 5 4]
 [5 5 6]]
"""

print(np.insert(a,1,[5],axis =0))  #0轴下,在位置 1 插入 [5,5]。
"""
[[1 2]
 [5 5]
 [3 4]
 [5 6]]
"""

Examples:

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

"""
第一个数组:
[[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]]
"""

8.3 numpy.append

  • numpy.append(arr, values, axis=None)
  • 将值附加到数组的末尾。
  • 参数说明:
    • arr:输入数组
    • values:要向arr添加的值,需要和arr形状相同(除了要添加的轴)
    • axis:默认为 None。当 axis 无定义时,是横向加成,返回总是为一维数组!当 axis 有定义的时候,分别为 0 和 1 的时候。当 axis 有定义的时候,分别为 01 的时候(列数要相同)。当axis1时,数组是加在右边(行数要相同)。

Examples:

np.append([1],[2])
"""
array([1, 2])
"""

np.append([1,2],[3])
"""
array([1, 2, 3])
"""

np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
"""
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
"""

8.4 numpy.resize

  • numpy.resize(a, new_shape)
  • 返回具有指定形状的新数组。

Examples:

a = np.array([[0,1],[2,3],[4,5]])
print(a)
"""
[[0 1]
 [2 3]
 [4 5]]
"""

print(np.resize(a,(2,3)))
"""
[[0 1 2]
 [3 4 5]]
"""

print(np.resize(a,(1,4)))
"""
[[0 1 2 3]]
"""

print(np.resize(a,(2,4)))
"""
[[0 1 2 3]
 [4 5 0 1]]
"""

print(np.resize(a,(2,6)))
"""
[[0 1 2 3 4 5]
 [0 1 2 3 4 5]]
"""

8.5 numpy.unique

  • numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
  • 用于去除数组中的重复元素。
  • 参数说明:
    • arr:输入数组,如果不是一维数组则会展开
    • return_index:如果为true,返回新列表元素在旧列表中的位置(下标),并以列表形式储
    • return_inverse:如果为true,返回旧列表元素在新列表中的位置(下标),并以列表形式储
    • return_counts:如果为true,返回去重数组中的元素在原数组中的出现次数

Examples:

np.unique([1, 1, 2, 2, 3, 3])
"""
array([1, 2, 3])
"""

a = np.array([[1, 1], [2, 3]])
np.unique(a)
"""
array([1, 2, 3])
"""

a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
np.unique(a, axis=0)
"""
array([[1, 0, 0],
       [2, 3, 4]])
"""

Examples:

a = np.array(['a', 'b', 'b', 'c', 'a'])
u, indices = np.unique(a, return_index=True)
print(u)
print(indices)
print(a[indices])
"""
['a' 'b' 'c']
[0 1 3]
['a' 'b' 'c']
"""

a = np.array([1, 2, 6, 4, 2, 3, 2])
u, indices = np.unique(a, return_inverse=True)
print(a)
print(u)
print(indices)
print(u[indices])
"""
[1 2 6 4 2 3 2]
[1 2 3 4 6]
[0 1 4 3 1 2 1]
[1 2 6 4 2 3 2]
"""

a = np.array([1, 2, 6, 4, 2, 3, 2])
values, counts = np.unique(a, return_counts=True)
print(a)
print(values)
print(counts)
print(np.repeat(values, counts))
"""
[1 2 6 4 2 3 2]
[1 2 3 4 6]
[1 3 1 1 1]
[1 2 2 2 3 4 6]
"""

8.6 numpy.trim_zeros

  • numpy.trim_zeros(filt, trim='fb')
  • 从一维数组或序列中修剪前导和/或尾随零。

Examples:

a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
print(np.trim_zeros(a))
"""
array([1, 2, 3, 0, 2, 1])
"""

print(np.trim_zeros(a, 'b'))  #’b‘代表删除后面的 0 
"""
array([0, 0, 0, ..., 0, 2, 1])
"""

print(np.trim_zeros(a, 'f')) #’f‘代表删除前面的 0 
"""
[1 2 3 0 2 1 0]
"""

print(np.trim_zeros([0, 1, 2, 0])) #默认代表删除前面和后面的 0 
"""
[1, 2]
"""

9 重新排列元素

9.1 numpy.flip

  • numpy.flip(m, axis=None)
  • 沿给定轴反转数组中元素的顺序。

Examples:

A = np.arange(8).reshape((2,2,2))
print(A)
#输出结果
"""
array([[[0, 1],
        [2, 3]],
       [[4, 5],
        [6, 7]]])
"""

print(np.flip(A, 0))
"""
array([[[4, 5],
        [6, 7]],
       [[0, 1],
        [2, 3]]])
"""

print(np.flip(A, 1))
"""
array([[[2, 3],
        [0, 1]],
       [[6, 7],
        [4, 5]]])
"""

Examples:

print(np.flip(A))
"""
array([[[7, 6],
        [5, 4]],
       [[3, 2],
        [1, 0]]])
"""

print(np.flip(A, (0, 2)))
"""
array([[[5, 4],
        [7, 6]],
       [[1, 0],
        [3, 2]]])
"""

9.2 numpy.fliplr

  • numpy.fliplr(m)
  • 沿轴 1(左/右)反转元素的顺序。

Examples:

A = np.diag([1.,2.,3.])
print(A)
"""
array([[1.,  0.,  0.],
       [0.,  2.,  0.],
       [0.,  0.,  3.]])
"""

print(np.fliplr(A))
"""
array([[0.,  0.,  1.],
       [0.,  2.,  0.],
       [3.,  0.,  0.]])
"""

Examples:

a = np.arange(9).reshape(3,3)
print(a)
"""
[[0 1 2]
 [3 4 5]
 [6 7 8]]
"""

print(np.fliplr(a))
"""
[[2 1 0]
 [5 4 3]
 [8 7 6]]
"""

9.3 numpy.flipud

  • numpy.flipud(m)
  • 沿轴 0(上/下)反转元素的顺序。

Examples:

a = np.arange(9).reshape(3,3)
print(a)
"""
[[0 1 2]
 [3 4 5]
 [6 7 8]]
"""

print(np.flipud(a))
"""
[[6 7 8]
 [3 4 5]
 [0 1 2]]
"""

9.4 numpy.roll

  • numpy.roll(a, shift, axis=None)
  • 沿给定轴滚动数组元素。

Examples:

x = np.arange(10)
print(x)
"""
[0 1 2 3 4 5 6 7 8 9]
"""

print(np.roll(x,2))
"""
[8 9 0 1 2 3 4 5 6 7]
"""

print(np.roll(x,-2))
"""
[2 3 4 5 6 7 8 9 0 1]
"""

Examples:

x = x.reshape((2,5))
print(x)
"""
[[0 1 2 3 4]
 [5 6 7 8 9]]
"""

print(np.roll(x,2))
"""
[[8 9 0 1 2]
 [3 4 5 6 7]]
"""

print(np.roll(x, -1))
"""
[[1 2 3 4 5]
 [6 7 8 9 0]]
"""

print(np.roll(x,2,axis = 0))
"""
[[0 1 2 3 4]
 [5 6 7 8 9]]
"""

print(np.roll(x,1,axis = 0))
"""
[[5 6 7 8 9]
 [0 1 2 3 4]]
"""

print(np.roll(x,2,axis = 1))
"""
[[3 4 0 1 2]
 [8 9 5 6 7]]
"""

Examples:

print(np.roll(x, (1, 1), axis=(1, 0)))
"""
[[9 5 6 7 8]
 [4 0 1 2 3]]
"""

print(np.roll(x, (2, 1), axis=(1, 0)))
"""
[[8 9 5 6 7]
 [3 4 0 1 2]]
"""

Reference:Array manipulation routines

posted @ 2022-02-12 16:24  多发Paper哈  阅读(398)  评论(0编辑  收藏  举报
Live2D