一、 改变数组维度
方法一: reshape() 不改变原数组
arr1 = np.arange(24)
arr1
输出:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
result1 = arr1.reshape(4,6)
输出:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
方法二 . 通过 shape 属性 ,直接更改原数组
arr2.shape = (4,3,2)
输出:
array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15],
[16, 17]],
[[18, 19],
[20, 21],
[22, 23]]])
方法三: 通过 resize() ,直接更改原数组
arr3 = np.arange(24)
arr3.resize(3,2,4)
输出:
array([[[ 0, 1, 2, 3],
[ 4, 5,