Simple reshape of numpy array: error: 'total size of new array must be unchanged'
Simple reshape of numpy array: 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)))
这是我将 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)))