Lesson6——NumPy 数组操作
1 基础操作
1.1 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')
- 在不更改其数据的情况下为数组赋予新形状。
Examples:
>>>a = np.zeros((3,2))
>>>np.reshape(a,(2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>>np.reshape(a,6)
array([0., 0., 0., 0., 0., 0.])
>>>np.reshape(np.arange(6).reshape(2,3),6,order='F')
array([0, 3, 1, 4, 2, 5])
>>>np.reshape(np.arange(6).reshape(2,3),6,order='C')
array([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
array([[1, 2, 3],
[4, 5, 6]])
>>>np.ravel(x)
array([1, 2, 3, 4, 5, 6])
>>>x.reshape(-1)
array([1, 2, 3, 4, 5, 6])
>>>np.ravel(x,order='F')
array([1, 4, 2, 5, 3, 6])
Examples:
>>>np.ravel(x.T)
array([1, 4, 2, 5, 3, 6])
>>>print(x.T)
[[1 4]
[2 5]
[3 6]]
>>>np.ravel(x.T,order = "A")
array([1, 2, 3, 4, 5, 6])
>>>print(x.T)
[[1 4]
[2 5]
[3 6]]
>>>np.ravel(x.T,order = "C")
array([1, 4, 2, 5, 3, 6])
>>>print(x.T)
[[1 4]
[2 5]
[3 6]]
>>>np.ravel(x.T,order = "K")
array([1, 2, 3, 4, 5, 6])
Examples:
>>>a = np.arange(12).reshape(2,3,2).swapaxes(1,2);
>>>print(a)
[[[ 0 2 4]
[ 1 3 5]]
[[ 6 8 10]
[ 7 9 11]]]
>>>a.ravel(order='C')
array([ 0, 2, 4, 1, 3, 5, 6, 8, 10, 7, 9, 11])
>>>a.ravel(order='F')
array([ 0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11])
>>>a.ravel(order='K')
array([ 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
#结果
array([[1, 2, 3],
[4, 5, 6]])
x.flat[3]
#结果
4
x.T
#结果
array([[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]]
#结果
array([3, 4])
x.flat[[3,4]]
#结果
array([4, 5])
2.4 numpy.ndarray.flatten
- ndarray.flatten(order='C')
- 返回折叠成一维的数组的副本。
- 方法
Examples:
x = np.array([[1,2],[3,4]])
x
#输出结果
array([[1, 2],
[3, 4]])
x.flatten()
#输出结果
array([1, 2, 3, 4])
x.flatten('F')
#输出结果
array([1, 3, 2, 4])
x.flatten('K')
#输出结果
array([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
#输出结果
(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)
- 向后滚动指定的轴,直到它位于给定位置。
Examples:
a = np.ones((3,4,5,6))
np.rollaxis(a,3,1).shape
#输出结果
(3, 6, 4, 5)
np.rollaxis(a,2).shape
#输出结果
(5, 3, 4, 6)
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]])
np.swapaxes(x,0,1)
#输出结果
array([[1],
[2],
[3]])
x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])
x
#输出结果
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
np.swapaxes(x,0,2)
#输出结果
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]])
Examples:
x = np.arange(24).reshape((2,3,4))
print(x.shape)
#输出结果
(2, 3, 4)
print(np.swapaxes(x,0,0))
print(np.swapaxes(x,0,0).shape)
#输出结果
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
(2, 3, 4)
print(np.swapaxes(x,0,1))
print(np.swapaxes(x,0,1).shape)
#输出结果
[[[ 0 1 2 3]
[12 13 14 15]]
[[ 4 5 6 7]
[16 17 18 19]]
[[ 8 9 10 11]
[20 21 22 23]]]
(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))
np.transpose(x).shape
#输出结果
(3, 2, 1)
x = np.arange(6).reshape((1,2,3))
print(np.transpose(x,(1,0,2)).shape)
print(np.transpose(x,(1,2,0)).shape)
#输出结果
(2, 1, 3)
(2, 3, 1)
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 中删除长度为 1 的轴。
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)
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)
5.3 numpy.hstack
- numpy.hstack(tup)
- 水平顺序堆叠数组(按列)。
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]])
5.4 numpy.vstack
- numpy.vstack(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]])
print(a.shape)
print(b.shape)
np.vstack((a,b))
#输出结果
(3, 1)
(3, 1)
array([[1],
[2],
[3],
[4],
[5],
[6]])
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]],
[[3, 4]]])
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 中的视图。
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])]
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.]],
[[4., 5.]]]),
array([[[2., 3.]],
[[6., 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.]]])]
7 平铺阵列
7.1 numpy.tile
- numpy.tile(A, reps)
- 通过重复 A 由 reps 给出的次数来构造一个数组。
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, 3, axis=1))
#输出结果
array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
print(np.repeat(x, [1, 2], axis=0))
#输出结果
array([[1, 2],
[3, 4],
[3, 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))
#输出结果
[[1 2 3]]
print(np.delete(arr,[1,2],axis = 1))
#输出结果
[[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 4 5 6 7 8 9]
reference
8.2 numpy.insert
- numpy.insert(arr, obj, values, axis=None)
- 在给定索引之前沿给定轴插入值.
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 2 3 4 5 6]
print(np.insert(a,1,5,axis =0))
#输出结果
[[1 2]
[5 5]
[3 4]
[5 6]]
print(np.insert(a,1,5,axis =1))
#输出结果
[[1 5 2]
[3 5 4]
[5 5 6]]
print(np.insert(a,1,[5],axis =0))
#输出结果
[[1 2]
[5 5]
[3 4]
[5 6]]
8.3 numpy.append
- numpy.append(arr, values, axis=None)
- 将值附加到数组的末尾。
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)
- 查找数组的唯一元素。
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'))
#输出结果
array([0, 0, 0, ..., 0, 2, 1])
print(np.trim_zeros(a, 'f'))
#输出结果
[1 2 3 0 2 1 0]
print(np.trim_zeros([0, 1, 2, 0]))
#输出结果
[1, 2]
9 重新排列元素
9.1 numpy.flip
- numpy.flip(m, axis=None)[source]
- 沿给定轴反转数组中元素的顺序。
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