区别:
- np.reshape()作用是将原来的数组变换形状,不改变数组元素数量,要求更改后的数组元素总数不变。
- np.resize()作用是改变数组的大小和形状,会改变数组元素数量,如果更改后的数组元素比原数组的多,则用原数组中的元素充填补齐。
实例:
In:
import numpy as np a = np.arange(9) b = np.reshape(a,(3,3)) c = np.resize(a,(5,5)) print(b) print() print(c)
Out:
原数组: [0 1 2 3 4 5 6 7 8]
reshape的结果:
[[0 1 2]
[3 4 5]
[6 7 8]]
数组形状为: (3, 3)
resize的结果:
[[0 1 2 3 4]
[5 6 7 8 0]
[1 2 3 4 5]
[6 7 8 0 1]
[2 3 4 5 6]]
数组的形状为: (5, 5)