重塑
import numpy as np
arr = np.arange(15).reshape((3,5))
print(arr)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
arr = np.arange(15).reshape(3,5)
print(arr)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
转置
arr = np.arange(18).reshape((3,6))
print(arr)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]
print(arr.T)
[[ 0 6 12]
[ 1 7 13]
[ 2 8 14]
[ 3 9 15]
[ 4 10 16]
[ 5 11 17]]
矩阵相乘
print(np.dot(arr.T,arr))
[[180 198 216 234 252 270]
[198 219 240 261 282 303]
[216 240 264 288 312 336]
[234 261 288 315 342 369]
[252 282 312 342 372 402]
[270 303 336 369 402 435]]
transpose
arr = np.arange(16).reshape(2,2,4)
print(arr)
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]]
print(arr.transpose((1,0,2)))
[[[ 0 1 2 3]
[ 8 9 10 11]]
[[ 4 5 6 7]
[12 13 14 15]]]
详细解释:
arr数组的内容为
- a[0][0] = [0, 1, 2, 3]
- a[0][1] = [4, 5, 6, 7]
- a[1][0] = [8, 9, 10, 11]
- a[1][1] = [12, 13, 14, 15]
transpose的参数为坐标,正常顺序为(0, 1, 2, ... , n - 1),
现在传入的为(1, 0, 2)代表a[x][y][z] = a[y][x][z],第0个和第1个坐标互换。原本shape是 2 2 4 传入后,还是2 2 4 shape不变
- a'[0][0] = a[0][0] = [0, 1, 2, 3]
- a'[0][1] = a[1][0] = [8, 9, 10, 11]
- a'[1][0] = a[0][1] = [4, 5, 6, 7]
- a'[1][1] = a[1][1] = [12, 13, 14, 15]
print(arr.transpose((2,1,0))) #原来100, 001
[[[ 0 8]
[ 4 12]]
[[ 1 9]
[ 5 13]]
[[ 2 10]
[ 6 14]]
[[ 3 11]
[ 7 15]]]
详细解释:
arr数组的内容为
- a[0][0][0] = [0]
- a[0][0][1] = [1]
- a[0][0][2] = [2]
- a[0][0][3] = [3]
- a[0][1][0] = [4]
- a[0][1][1] = [5]
- a[0][1][2] = [6]
- a[0][1][3] = [7]
- a[1][0][0] = [8]
- a[1][0][1] = [9]
- a[1][0][2] = [10]
- a[1][0][3] = [11]
- a[1][1][0] = [12]
- a[1][1][1] = [13]
- a[1][1][2] = [14]
- a[1][1][3] = [15]
transpose的参数为坐标,正常顺序为(0, 1, 2, ... , n - 1),
现在传入的为(2, 1, 0)代表a[x][y][z] = a[z][y][x],第0个和第2个坐标互换。原来shape是 2 2 4 变化后为4 2 2 shape产生变化
- a'[0][0][0] = a[0][0][0] = [0]
- a'[0][0][1] =a[1][0][0] = [8]
- a'[0][1][0] =a[0][1][0] = [4]
- a'[0][1][1] =a[1][1][0] = [12]
- a'[1][0][0] = a[0][0][1] = [1]
- a'[1][0][1] =a[1][0][1] = [9]
- a'[1][1][0] =a[0][1][1] = [5]
- a'[1][1][1] =a[1][1][1] = [13]
- a'[2][0][0] = a[0][0][2] = [2]
- a'[2][0][1] =a[1][0][2] = [10]
- a'[2][1][0] =a[0][1][2] = [6]
- a'[2][1][1] =a[1][1][2] = [14]
- a'[3][0][0] = a[0][0][3] = [3]
- a'[3][0][1] =a[1][0][3] = [11]
- a'[3][1][0] =a[0][1][3] = [7]
- a'[3][1][1] =a[1][1][3] = [15]