先生成一个倒序的数组
a = np.arange(12,0,-1)
array([12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
当对数组reshape时代表改变数组的形状为一维、二维、三维、n维
1. 二维:
一个为确定的数,另一个为-1
a.reshape(2, -1)
array([[12, 11, 10, 9, 8, 7],
[ 6, 5, 4, 3, 2, 1]])
a.reshape(-1, 3)
array([[12, 11, 10],
[ 9, 8, 7],
[ 6, 5, 4],
[ 3, 2, 1]])
但是给的确定的数值不是数组总元素个数的合数会报错
比如:12的合数为2、3、4、6,但是5就不是
a.reshape(5, -1)
ValueError: cannot reshape array of size 12 into shape (5,newaxis)
2. 一维: 只有一个-1
时会变为一维
3. 三维:(-1, 2, 3) 此时可以计算出-1位置代表 2
a.reshape(-1,2,3)
array([[[12, 11, 10],
[ 9, 8, 7]],
[[ 6, 5, 4],
[ 3, 2, 1]]])