numpy.transpose 详解

矩阵转置变换

二维

01
02
03
04
05
06
07
08
09
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)

结果

01
02
03
04
05
06
07
08
09
10
11
x
 [0 1 2 3]
x1       
 [[0 1]  
 [2 3]]  
x2       
 [[0 2]  
 [1 3]]  
x3
 [[0 2]
 [1 3]]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
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)

结果

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 @   wuyuan2011woaini  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示