0轴是行,1轴是列,2轴是纵深
数组的shape维度是(4,3,2),元组的索引为 [ 0,1,2 ]
假设维度是(2,3),元组的索引为[0,1]
假设维度是(4,) 元组的索引为[0]
总结:凡是提到轴,先看数组的维度,有几维就有几个轴
沿轴切片
import numpy as np
a = np.array([ [1,2,3] , [4,5,6] , [7,8,9] ])
print(a[:2]) #表示切取0轴上的索引[0]和索引[1]
[[1 2 3]
[4 5 6]]
import numpy as np
a = np.array([ [1,2,3] , [4,5,6] , [7,8,9] ])
print(a[:2,1:]) #:2表示切取0轴上的索引[0]和索引[1],1:表示切取1维(1轴)上的索引[1] 和索引[2]
[[2 3]
[5 6]]
transpose方法 【行列转置】
import numpy as np
a = np.arange(20).reshape(4,5)
print(a)
print(a.transpose())
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
------------------------------
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]
swapaxes方法 【轴转置】
import numpy as np
a = np.arange(20).reshape(4,5)
print(a)
print('-'*30)
print(a.swapaxes(1,0)) #将0轴1轴互换
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
------------------------------
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]
numpy 轴: