代码之家  ›  专栏  ›  技术社区  ›  Shlomi Schwartz

NumPy-计算直方图交点

  •  3
  • Shlomi Schwartz  · 技术社区  · 6 年前

    以下数据表示分成13个箱的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
    

    2 回复  |  直到 6 年前
        1
  •  6
  •   mdoc-2011    5 年前

    def histogram_intersection(h1, h2):
        sm = 0
        for i in range(13):
            sm += min(h1[i], h2[i])
        return sm
    
        2
  •  1
  •   B. M.    6 年前

    首先要注意的是:在你的数据箱中是范围,在你的算法中是数字。你必须为此重新定义垃圾箱。

    此外, min(bins[i]*h1[i], bins[i]*h2[i]) bins[i]*min(h1[i], h2[i]) 因此,可以通过以下方法获得结果:

    hists=pandas.read_clipboard(index_col=0) # your data
    bins=arange(-4,112,9)   #  try for bins but edges are different here
    mins=hists.min('rows')
    intersection=dot(mins,bins) 
    
        3
  •  0
  •   Mark Setchell    5 年前

    #!/usr/bin/env python3
    
    import numpy as np
    
    A = np.array([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 = np.array([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])
    
    def histogram_intersection(h1, h2):
        sm = 0
        for i in range(13):
            sm += min(h1[i], h2[i])
        return sm
    
    print(histogram_intersection(A,B))
    print(np.sum(np.minimum(A,B)))
    

    输出

    88.44792356099998
    88.447923561
    

    但如果你计时,Numpy只需要60%的时间:

    %timeit histogram_intersection(A,B)
    5.02 µs ± 65.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
    %timeit np.sum(np.minimum(A,B))
    3.22 µs ± 11.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)