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: 桔子codeimport numpy as npa = np.arange(24)b = a.reshape(3,5)print(b)=========运行结果:---------------
错误提示:
对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)
错误原因:
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等编程知识
更多推荐
所有评论(0)