【问题标题】:How to break a list into couple lists如何将一个列表分成几个列表
【发布时间】:2021-02-04 03:03:57
【问题描述】:

假设我有一个带有size=(8,64,1,60,60) 的列表,并且想将其分解为(4,2,64,1,60,60),然后将它们沿轴 1 相加。我尝试了下面的代码,但它出现了错误:

'list' object has no attribute 'reshape'.

请注意,我希望将预测保留为列表,并且不想将其更改为数组。

predictions=list(np.random.randint(5,size=(8,64,1,60,60)))
predictions_sum = predictions.reshape(4,2, *predictions.shape[1:]).sum(axis=1)

【问题讨论】:

    标签: python arrays list reshape


    【解决方案1】:

    您将 Python 的内置 list 类型与 numpyarray 混淆了。试试这个:

    predictions=np.array(np.random.randint(5,size=(8,64,1,60,60)))
    predictions_sum = predictions.reshape(4,2, *predictions.shape[1:]).sum(axis=1)
    

    【讨论】:

    • 我提到我想将预测保留为一个列表,因为我使用 predictions=[ ] 作为 5D 空列表来填充上面的矩阵。
    • 很公平,但 list 不是 numpy 类型,因此没有 .reshape 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2019-07-04
    相关资源
    最近更新 更多