我有一个对象列表,其中有一个属性(attributions
),我希望将其平均化(按元素)。最好的方法是什么?你知道吗
我将对象(ExplanationObjects
)放在一个列表attr
中,每个对象都有一个属性attributions
,我希望以元素方式将其平均化。例如,如果我有:
a.attribution=[[2,1],[4,6]]
以及
b.attribution=[[4,3],[8,8]]
我想得到
[[3,2],[6,7]]
现在,我用
(sum(a.attribution for a in attrs))/len(attrs)
这是最好的方法,还是有其他方法(numpy首选)你会建议?你知道吗
如果将属性转换为numpy数组,则可以这样做。如果你在你的类中创建一个数组,这看起来会更整洁。我不知道你能不能做到,所以我的例子不能。你知道吗
我用了几种不同的方法。您可以加上2并除以2,也可以使用
numpy.mean
。你知道吗输出
相关问题 更多 >
编程相关推荐