版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/tsyccnh/article/details/76177750
numpy reshape 函数的基本使用
import numpy as np
mat1 = np.array([
[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]],
[[2,4,6],
[8,10,12],
[14,16,18],
[20,22,24]]
])
mat2 = mat1.reshape(-1) # 转换成一个一维行向量
mat3 = mat1.reshape(-1,1,4,3)# 保持第一维不变,在第一维后增加一位,其他维不变
print('mat1:',mat1.shape)
print(mat1)
print('mat2:',mat2.shape)
print(mat2)
print('mat3:',mat3.shape)
print(mat3)
输出:
mat1: (2, 4, 3)
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 2 4 6]
[ 8 10 12]
[14 16 18]
[20 22 24]]]
mat2: (24,)
[ 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24]
mat3: (2, 1, 4, 3)
[[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]]
[[[ 2 4 6]
[ 8 10 12]
[14 16 18]
[20 22 24]]]]