import numpy as np
a1 = np.arange(0, 6)
print(a1)
[0 1 2 3 4 5]
不改变原数组维度
reshape 不会改变原始数组维度
a2 = a1.reshape(2,3)
print(a2)
[[0 1 2]
[3 4 5]]
print(a1)
[0 1 2 3 4 5]
ravel用于将一个多维的数组展成一维数组,不会改变原始数组维度
a3 = a2.ravel()
print(a3)
[0 1 2 3 4 5]
print(a2)
[[0 1 2]
[3 4 5]]
flatten的作用和ravel一样,不会改变原始数组维度
a4 = a2.flatten()
print(a4)
[0 1 2 3 4 5]
print(a2)
[[0 1 2]
[3 4 5]]
改变原数组维度大小
.shape
a1.shape = (3, 2)
print(a1)
[[0 1]
[2 3]
[4 5]]
resize和reshape使用方法一样,但是resize是对原数组进行操作,而reshape并不改变原数组
a1.resize(2,3)
print(a1)
[[0 1 2]
[3 4 5]]