python numpy读取数据时出现错误,Python错误集锦:对numpy数组进行变形操作时,提示ValueError: cannot reshape array of size 24 into ...

错误提示:

对numpy数组进行变形操作时,提示ValueError: cannot reshape array of size 24 into shape (3,5):

#juzicode.com ;#VX: 桔子code

import numpy as np

a = np.arange(24)

b = a.reshape(3,5)

print(b)

=========运行结果:

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

in

2 import numpy as np

3 a = np.arange(24)

----> 4 b = a.reshape(3,5)

5 print(b)

ValueError: cannot reshape array of size 24 into shape (3,5)

92de03fae5e7b8127c057b39764578c2.png

错误原因:

1、原始数组包含的元素个数为24,试图用reshape修改后的数组为3×5的二维数组,试图生成的数组的元素为3×5=15个,不等于原始数组的元素个数24。

解决方法:

1、改变后的数组形状的每个轴的长度乘积必须等于原始数据元素的个数,上面这个例子中,如果要修改为二维数组,修改后的形状可以是(3,8)或(4,6)等形式:

#juzicode.com ;#VX: 桔子code

import numpy as np

a = np.arange(24)

b = a.reshape(3,8)

print(b)

=====运行结果:

[[ 0 1 2 3 4 5 6 7]

[ 8 9 10 11 12 13 14 15]

[16 17 18 19 20 21 22 23]]

扩展内容:

关注微信公众号:“桔子code”,欢迎后台留言撩我,我会尽我所能为你解惑Python,C等编程知识

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值