【问题标题】:Simple reshape of numpy array: error: 'total size of new array must be unchanged'numpy 数组的简单整形:错误:'新数组的总大小必须保持不变'
【发布时间】:2019-04-25 14:28:42
【问题描述】:

这是我将 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'

【问题讨论】:

  • 如果我的回答有帮助,请告诉我
  • 你没有仔细阅读文档
  • @seralouk 是的,它准确地回答了我的问题。谢谢!

标签: python numpy reshape numpy-ndarray


【解决方案1】:

你没有使用你应该使用的功能。

只需使用这个:

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)))

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2020-11-18
    • 2019-01-18
    相关资源
    最近更新 更多