Python numpy数组的简单整形:错误:';新阵列的总大小必须保持不变';

Python numpy数组的简单整形:错误:';新阵列的总大小必须保持不变';,python,numpy,reshape,numpy-ndarray,Python,Numpy,Reshape,Numpy Ndarray,这是一个非常简单的问题版本,我正在将一个40*1的数组重塑为一个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' 您没有按应使用的方式使用该

这是一个非常简单的问题版本,我正在将一个40*1的数组重塑为一个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))

完整代码:

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

如果我的回答有帮助,请告诉我您没有仔细阅读文档enough@seralouk是的,它完全回答了我的问题。谢谢