数组转置和轴对称
转置(transpose)是重塑的一种特殊形式,它返回的是源数据的视图,不会进行任何复制操作,数组不仅有transpose方法,还要一个特殊的T属性。
(1)transpose
(2)T
(3)swapaxes
1 import numpy as np 2 from pprint import pprint 3 arr = np.arange(15).reshape((3,5)) 4 pprint(arr) 5 pprint(arr.T) 6 pprint(arr.transpose()) 7 pprint(arr.swapaxes(0,1))
运行结果:
1 array([[ 0, 1, 2, 3, 4], 2 [ 5, 6, 7, 8, 9], 3 [10, 11, 12, 13, 14]]) 4 array([[ 0, 5, 10], 5 [ 1, 6, 11], 6 [ 2, 7, 12], 7 [ 3, 8, 13], 8 [ 4, 9, 14]]) 9 array([[ 0, 5, 10], 10 [ 1, 6, 11], 11 [ 2, 7, 12], 12 [ 3, 8, 13], 13 [ 4, 9, 14]]) 14 array([[ 0, 5, 10], 15 [ 1, 6, 11], 16 [ 2, 7, 12], 17 [ 3, 8, 13], 18 [ 4, 9, 14]])