如何将列表分解为一对列表

问题描述

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

'list' object has no attribute 'reshape'.

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

predictions=list(np.random.randint(5,size=(8,60)))
predictions_sum = predictions.reshape(4,*predictions.shape[1:]).sum(axis=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)