我有一维数组,我想numpy bincount用来创建直方图.它工作正常,但我希望它忽略NaN值.
histogram = np.bincount(distancesArray, weights=intensitiesArray) / np.bincount(distancesArray)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
谢谢你的帮助!
这就是我认为你的问题所在:
import numpy
w = numpy.array([0.3, float("nan"), 0.2, 0.7, 1., -0.6]) # weights
x = numpy.array([0, 1, 1, 2, 2, 2])
numpy.bincount(x, weights=w)
#>>> array([ 0.3, nan, 1.1])
Run Code Online (Sandbox Code Playgroud)
解决方案就是使用索引来仅保留非纳米权重:
keep = ~numpy.isnan(w)
numpy.bincount(x[keep], weights=w[keep])
#>>> array([ 0.3, 0.2, 1.1])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2695 次 |
| 最近记录: |