average of a number of arrays with numpy without considering zero values

天大地大妈咪最大 提交于 2019-12-01 00:36:58
>>> import numpy as np
>>> a = np.array([153, 186, 0, 258])
>>> b = np.array([156, 136, 156, 0])
>>> c = np.array([193, 150, 950, 757])
>>> [np.mean([x for x in s if x]) for s in np.c_[a, b, c]]
[167.33333333333334, 157.33333333333334, 553.0, 507.5]

Or maybe a nicer alternative:

>>> A = np.vstack([a,b,c])
>>> np.average(A, axis=0, weights=A.astype(bool))
array([ 167.33333333,  157.33333333,  553.        ,  507.5       ])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!