作用:在不改变数据内容的情况下,改变一个数组的格式
1. reshape(row,col)可以根据指定的数值将数据转换为特定的行数和列数
r = np.random.randint(0, 10, size=(3, 4))
print(r.reshape(3, 4))
[[2 3 1 9]
[4 5 5 0]
[3 8 3 9]]
reshape(3,4)----> 输出3行4列的矩阵
2. reshape(1,-1) 是输出1行,不指定列数的矩阵;reshape(-1,1)则是输出1列,不指定行数的矩阵;在官方文档中,-1被理解为unspecified value,未指定为给定的行数(列数)
print(r.reshape(1, -1))
[[1 6 3 3 8 6 1 6 4 4 2 3]]
print(r.reshape(-1, 1))
[[3]
[3]
[1]
[0]
[1]
[1]
[2]
[6]
[7]
[4]
[4]
[1]]
3. reshape(row,col,order=),这里的order参数可以选 A,C,F,默认是C。
C 是按照行顺序, F 是按照列顺序,A 就是按照数据在内存中存储的顺序来。
print(r.reshape((3, 4), order='A'))
print(r.reshape((3, 4), order='C'))
print(r.reshape((3, 4), order='F'))
[[2 3 3 5 0 7]
[7 7 4 7 2 8]] # 原始矩阵
[[2 3 3 5]
[0 7 7 7]
[4 7 2 8]] # order='A'
[[2 3 3 5]
[0 7 7 7]
[4 7 2 8]] # oeder='C'
[[2 7 5 2]
[7 3 7 7]
[3 4 0 8]] # order='F'