函数 | 描述 |
---|---|
transpose | 对换数组的维度 |
ndarray.T | 转置 |
rollaxis | 向后滚动指定的轴 |
swapaxes | 对换数组的两个轴 |
import numpy as np
arr0 = np.arange(12).reshape(3, 4)
arr2 = np.transpose(arr0)
arr3 = arr0.T
print('arr0 原数组:\n', arr0,'\n')
print('arr2 维度对换之后:\n', arr2,'\n')
print('arr3 数组转置:\n', arr3,'\n')
'''
arr0 原数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
arr2 维度对换之后:(显然与转置效果arr3相同)
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
arr3 数组转置:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
'''
arr1 = np.arange(18).reshape(2,3,3)
arr4_0 = np.rollaxis(arr1,0,2)
arr4_1 = np.rollaxis(arr1,2,1)
print('arr1 原数组:\n', arr1,'\n')
print('arr4_0 向后滚动特定的轴到一个特定位置:\n', arr4_0,'\n')
print('arr4_1 向后滚动特定的轴到一个特定位置:\n', arr4_1,'\n')
arr5_0 = np.swapaxes(arr1, 0, 1)
arr5_1 = np.swapaxes(arr1, 1, 2)
print('arr5_0交换数组的两个轴:\n', arr5_0,'\n')
print('arr5_1交换数组的两个轴:\n', arr5_1,'\n')
'''
arr1 原数组:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]]
arr4_0 向后滚动特定的轴到一个特定位置:(与arr5_0 效果相同)
[[[ 0 1 2]
[ 9 10 11]]
[[ 3 4 5]
[12 13 14]]
[[ 6 7 8]
[15 16 17]]]
arr4_1 向后滚动特定的轴到一个特定位置:(与arr5_1 效果相同)
[[[ 0 3 6]
[ 1 4 7]
[ 2 5 8]]
[[ 9 12 15]
[10 13 16]
[11 14 17]]]
arr5_0交换数组的两个轴:
[[[ 0 1 2]
[ 9 10 11]]
[[ 3 4 5]
[12 13 14]]
[[ 6 7 8]
[15 16 17]]]
arr5_1交换数组的两个轴:
[[[ 0 3 6]
[ 1 4 7]
[ 2 5 8]]
[[ 9 12 15]
[10 13 16]
[11 14 17]]]
'''
import numpy as np
def flip180(arr):
new_arr = arr.reshape(arr.size)
new_arr = new_arr[::-1]
new_arr = new_arr.reshape(arr.shape)
return new_arr
def flip90_left(arr):
new_arr = np.transpose(arr)
new_arr = new_arr[::-1]
return new_arr
def flip90_right(arr):
new_arr = arr.reshape(arr.size)
new_arr = new_arr[::-1]
new_arr = new_arr.reshape(arr.shape)
new_arr = np.transpose(new_arr)[::-1]
return new_arr
arr0 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
flip_180 = flip180(arr0)
left_90 = flip90_left(arr0)
right_90 = flip90_right(arr0)
print('===== flip_180 ====\n',flip_180,'\n')
print('===== left_90 =====\n',left_90,'\n')
print('===== right_90 =====\n',right_90,'\n')
'''
===== flip_180 ====
[[9 8 7]
[6 5 4]
[3 2 1]]
===== left_90 =====
[[3 6 9]
[2 5 8]
[1 4 7]]
===== right_90 =====
[[7 4 1]
[8 5 2]
[9 6 3]]
'''
flip() (in module numpy)
fliplr() (in module numpy)
flipud() (in module numpy)
>>> A = np.array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> flip(A, 0)
array([[[4, 5],
[6, 7]],
[[0, 1],
[2, 3]]])
>>> flip(A, 1)
array([[[2, 3],
[0, 1]],
[[6, 7],
[4, 5]]])
>>> np.flip(A)
array([[[7, 6],
[5, 4]],
[[3, 2],
[1, 0]]])
>>> np.flip(A, (0, 2))
array([[[5, 4],
[7, 6]],
[[1, 0],
[3, 2]]])
>>> A = np.random.randn(3,4,5)
>>> np.all(flip(A,2) == A[:,:,::-1,...])
True
flipud: (==flip(m, 1) )
>>> A = np.diag([1.0, 2, 3])
>>> A
array([[ 1., 0., 0.],
[ 0., 2., 0.],
[ 0., 0., 3.]])
>>> np.flipud(A)
array([[ 0., 0., 3.],
[ 0., 2., 0.],
[ 1., 0., 0.]])
>>> A = np.random.randn(2,3,5)
>>> np.all(np.flipud(A) == A[::-1,...])
True
>>> np.flipud([1,2])
array([2, 1])
fliplr: (==flip(m, 0))
>>> A = np.diag([1.,2.,3.])
>>> A
array([[ 1., 0., 0.],
[ 0., 2., 0.],
[ 0., 0., 3.]])
>>> np.fliplr(A)
array([[ 0., 0., 1.],
[ 0., 2., 0.],
[ 3., 0., 0.]])
>>> A = np.random.randn(2,3,5)
>>> np.all(np.fliplr(A) == A[:,::-1,...])
True
特别鸣谢:
https://www.cnblogs.com/xiaoniu-666/p/11123560.html
https://blog.csdn.net/kane7csdn/article/details/83928848