numpy.transpose 详解

矩阵转置变换

二维

import numpy as np
x = np.arange(4)
print("x\n",x)
x1=x.reshape((2,2))
print("x1\n",x1)
x2=x1.transpose()
print("x2\n",x2)
x3=x1.transpose(1,0)
print("x3\n",x3)

结果

x
 [0 1 2 3]
x1        
 [[0 1]   
 [2 3]]   
x2        
 [[0 2]   
 [1 3]]   
x3
 [[0 2]
 [1 3]]

import numpy as np
x = np.arange(24)
print("x\n",x)
x1=x.reshape((2,3,4))
print("x1\n",x1)
x2=x1.transpose()
print("x2\n",x2)
print("x2.shape",x2.shape)
x3=x1.transpose(2,1,0)
print("x3\n",x3)
print("x3.shape",x3.shape)
x4=x1.transpose(1,0,2)
print("x4\n",x4)
print("x4.shape",x4.shape)

结果

x
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
x1
 [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
x2
 [[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]
x2.shape (4, 3, 2)
x3
 [[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]
x3.shape (4, 3, 2)
x4
 [[[ 0  1  2  3]
  [12 13 14 15]]

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

 [[ 8  9 10 11]
  [20 21 22 23]]]
x4.shape (3, 2, 4)
posted @ 2022-01-28 16:38  wuyuan2011woaini  阅读(38)  评论(0编辑  收藏  举报