python - NumPy - 计算直方图交集

标签 python numpy statistics histogram

以下数据,表示分成 13 个 bin 的 2 个给定直方图:

key 0   1-9 10-18   19-27   28-36   37-45   46-54   55-63   64-72   73-81   82-90   91-99   100
A   1.274580708 2.466224824 5.045757621 7.413716262 8.958855646 10.41325305 11.14150951 10.91949012 11.29095648 10.95054297 10.10976255 8.128781795 1.886568472
B   0   1.700493692 4.059243006 5.320899616 6.747120132 7.899067471 9.434997257 11.24520022 12.94569391 12.83598464 12.6165661  10.80636314 4.388370817

enter image description here

我正在尝试按照 this article 来计算这两个直方图之间的交集,使用此方法:

def histogram_intersection(h1, h2, bins):
   bins = numpy.diff(bins)
   sm = 0
   for i in range(len(bins)):
       sm += min(bins[i]*h1[i], bins[i]*h2[i])
   return sm

由于我的数据已经计算为直方图,我不能使用 numpy 内置函数,所以我无法为该函数提供必要的数据。

如何处理我的数据以适应算法?

最佳答案

由于两个直方图的 bin 数量相同,您可以使用:

def histogram_intersection(h1, h2):
    sm = 0
    for i in range(13):
        sm += min(h1[i], h2[i])
    return sm

关于python - NumPy - 计算直方图交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184242/

相关文章:

python - 如何设置yacc of ply的优先级

python - 需要帮助调试 python html 生成器

python - 使用广播根据向量中的元素乘以矩阵行?

r - 如何让 R 以指数表示法读取一列数字?

类似于R的C概率库

python - 如何在 VSCode 的 Pane 中显示 python 环境对象?

python - Django 将反向 url 获取到变量中

python - 同一个列表的多次迭代

python - 返回矩阵/数组的最常见值(模式)

numpy - Python 中 cor.test 的 R 等价物