gpt4 book ai didi

python - numpy 数组 : error: 'total size of new array must be unchanged' 的简单 reshape

转载 作者:太空宇宙 更新时间:2023-11-03 15:39:28 25 4
gpt4 key购买 nike

这是我将 40*1 数组 reshape 为 20*2 数组的问题的一个非常简单的版本。这里出了什么问题?

import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
#reshape to 2*20
print(np.reshape(x,2,20))
#returns error: 'total size of new array must be unchanged'

最佳答案

您没有按应使用的方式使用该功能。

只需使用这个:

np.reshape(x,(2,20))

Documentation here

完整代码:

import numpy as np
x=np.linspace(1,20,40)
#confirm length is 40
print(np.shape(x))
print(np.reshape(x,(2,20)))

关于python - numpy 数组 : error: 'total size of new array must be unchanged' 的简单 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com