繁体   English   中英

Python:提高 class 中欧几里得距离计算的速度

[英]Python: Improve the speed of Euclidean distance calculation in a class

我有一个 class 组件,用于计算 2 个字典中 arrays 中最后一个元素之间的欧几里得距离。 一个字典包含 blob 的跟踪轨迹( r ),另一个字典包含 blob 的更新值( b )。 class的方法基于欧几里得距离寻找出现或消失的轨迹。 最后,他们将r字典重新排序为与b字典的最佳匹配。

我测试了这个colab 笔记本中的功能,它按预期工作,但是当我在我的代码上实现它时,程序变慢了。

  1. 有没有办法可以提高这个 class 的速度?
  2. 有没有更好的方法来解决这个问题? 它是什么?

谢谢你。

from scipy.spatial import distance as dist

class finder:

    def disappeared(self,r,b):
        values = {}
        indexes = {}
        diss = {}
        new_results = {}
        new_positions = {}

        le = len(r) - len(b)       

        for i in r:
            xr = r[i]["x"][-1]
            yr = r[i]["y"][-1]
            
            for k in b:
                xb = b[k]["x"][-1]
                yb = b[k]["y"][-1]
              
                D = dist.cdist([(xb,yb)],[(xr,yr)])
               
                values[str(i) +"/" + str(k)] = D
                indexes[str(i) +"/" + str(k)] = (i,k)

            if le > 0:
                le -= 1
                  
                maxval = max(values,key=values.get)
        
                r_ind = indexes[maxval][0]
                b_ind = indexes[maxval][1]

                print("Found Disappeared", maxval) 
  
                diss[r_ind] = r[r_ind]
            
            else:
                minval = min(values,key=values.get)
                r_ind = indexes[minval][0]
                b_ind = indexes[minval][1]
                new_positions[b_ind] = r[r_ind]
                
                del values[minval]
         
        for m,n in enumerate(new_positions):
            new_results[m] = new_positions[n]

        return(new_results,diss)

    def appeared(self,r,b):
        values = {}
        indexes = {}
        appr = {}
        new_results = {}
        new_positions = {}

        le = len(b) - len(r)       

        for i in b:

            xb = b[i]["x"][-1]
            yb = b[i]["y"][-1]

            for k in r:

                xr = r[k]["x"][-1]
                yr = r[k]["y"][-1]
              
                D = dist.cdist([(xr,yr)],[(xb,yb)])
               
                values[str(k) +"/" + str(i)] = D
                indexes[str(k) +"/" + str(i)] = (k,i)

            if le > 0:
                le -= 1
                  
                maxval = max(values,key=values.get)
        
                r_ind = indexes[maxval][0]
                b_ind = indexes[maxval][1]

                print("Found Appeared", maxval) 
  
                appr[b_ind] = b[b_ind]
                new_positions[r_ind] = b[b_ind]
            
            else:
                minval = min(values,key=values.get)
                r_ind = indexes[minval][0]
                b_ind = indexes[minval][1]
                new_positions[b_ind] = r[r_ind]
                
                del values[minval]
         
        for m,n in enumerate(new_positions):
            new_results[m] = new_positions[n]

        return(new_results)

大部分时间可能都花在访问字典和格式化字符串上。

这里有一些你可以做的事情来优化disappeared()

只访问b的值一次:

 # at start of function ...
 lastB = [ (k,v["x"][-1],v["y"][-1]) for k,v in b.items() ]

 ...

 for k,xb,yb in lastB:  # replaces for k in b: and the assignments of xb,yb
     
     ...

访问r时获取值和键:

 for i,v in r.items():
     xr = v["x"][-1]
     yr = v["y"][-1]

使用元组而不是字符串作为values ,您根本不需要indexes

 # index it with a tuple
 values[(k,i)]  = D
 
 ...

 # replace the whole maxval logic.
 r_ind,b_ind,_ = max(values.items(),key=lambda kv:kv[1])     

 ...

 # replace the whole minval logic.
 r_ind,b_ind,_ = min(values.items(),key=lambda kv:kv[1])
 ...
 del values[r_ind,b_ind]     

无需重新访问每个键即可生成新结果:

 new_result = dict(enumerate(new_positions.values()))

可以对appeared()进行相同的改进,因为它几乎是相同的。

这段代码真的有效吗? 这些行看起来完全错误:

       for i in r:
            xr = r[i]["x"][-1]
            yr = r[i]["y"][-1]

ir的元素。 您不会将其用作r的索引。 当然应该是:

       for i in r:
            xr = i["x"][-1]
            yr = i["y"][-1]

对于for k in b循环也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM