np.reshape中-1详解

先生成一个倒序的数组

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]]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值