Python 提高元组距离计算算法的时间效率

Python 提高元组距离计算算法的时间效率,python,algorithm,list,distance,point,Python,Algorithm,List,Distance,Point,我有一个算法,可以计算每个点p(我的坐标值在元组中表示)到元组列表中每个其他元组的距离 i = 0 distanceList = [] for p in range(len(centerList)): while i < len(centerList): print centerList[p], centerList[i], getDistance(centerList[p], centerList[i]) distance = getDistanc

我有一个算法,可以计算每个点
p
(我的坐标值在元组中表示)到元组列表中每个其他元组的距离

i = 0
distanceList = []
for p in range(len(centerList)):
    while i < len(centerList):
        print centerList[p], centerList[i], getDistance(centerList[p], centerList[i])
        distance = getDistance(centerList[p], centerList[i])
        if distance < 20:
            distanceList.append(distance)
        i += 1
    i = p + 2
要点清单:

centerList = [(54, 2991),
            (1717, 2989),
            (1683, 2991),
            (1604, 2991),
            (114, 2991),
            (919,222),
            (930,233)]
距离function:

def getDistance(p0, p1):
    return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
计算点
p
到元组列表中每隔一点的距离的算法

i = 0
distanceList = []
for p in range(len(centerList)):
    while i < len(centerList):
        print centerList[p], centerList[i], getDistance(centerList[p], centerList[i])
        distance = getDistance(centerList[p], centerList[i])
        if distance < 20:
            distanceList.append(distance)
        i += 1
    i = p + 2
i=0
距离列表=[]
对于范围内的p(len(中心列表)):
而i
我当前的算法以一种不冗余的方式递增,但在其当前状态下,对于现实的应用程序来说,它太野蛮了。我的问题在于,我实际的
中心列表
包含数千个元组

如何提高此元组比较算法的时间效率?

您可以结合
numpy
的布尔索引进行计算:

>>> from sklearn.metrics import euclidean_distances
>>> import numpy as np
>>> centerList = np.array(centerList)
>>> distances = euclidean_distances(centerList)
>>> distances[distances<20]
array([  0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,  15.55634919,  15.55634919,   0.        ])
与其他计算方法相比,该公式有两个优点 距离。首先,它在处理 稀疏数据。第二,如果一个论点不同,但另一个论点不变 不变,则可以预先计算点(x,x)和/或点(y,y)

您可以结合
numpy
的布尔索引进行计算:

>>> from sklearn.metrics import euclidean_distances
>>> import numpy as np
>>> centerList = np.array(centerList)
>>> distances = euclidean_distances(centerList)
>>> distances[distances<20]
array([  0.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   0.        ,  15.55634919,  15.55634919,   0.        ])
与其他计算方法相比,该公式有两个优点 距离。首先,它在处理 稀疏数据。第二,如果一个论点不同,但另一个论点不变 不变,则可以预先计算点(x,x)和/或点(y,y)


仅使用
numpy

import numpy

centerList = [(54, 2991), (1717, 2989), (1683, 2991), (1604, 2991), (114, 2991), (919,222), (930,233)]
centerList = numpy.array(centerList)

def getDistance(p0,p1):
    return numpy.linalg.norm(p0-p1)

将返回与
getDistance
函数相同的结果。

仅使用
numpy

import numpy

centerList = [(54, 2991), (1717, 2989), (1683, 2991), (1604, 2991), (114, 2991), (919,222), (930,233)]
centerList = numpy.array(centerList)

def getDistance(p0,p1):
    return numpy.linalg.norm(p0-p1)

将返回与
getDistance
函数相同的结果。

看起来您正在尝试计算每对点之间的距离。这本质上是O(n^2),所以你可能想并行化,你可以先去掉sqrt,我想你的算法中有一些地方被破坏了
distance=getDistance(centerList[p],centerList[i])
第一次迭代比较
centerList[0]
centerList[0]
。这不会发生在后续迭代中,
i
将是
i=p+2
。第一个iter:
getDistance(中心列表[0],中心列表[0])
,第二个iter:
getDistance(中心列表[1],中心列表[2])
。。。为什么?从数学上讲,比较同一点的距离为零。如果x和y和距离x=y,那么从x到y的距离是零。是的,很好。这就是为什么我运行
如果距离<20且距离>0,那么看起来您正试图计算每对点之间的距离。这本质上是O(n^2),所以你可能想并行化,你可以先去掉sqrt,我想你的算法中有一些地方被破坏了
distance=getDistance(centerList[p],centerList[i])
第一次迭代比较
centerList[0]
centerList[0]
。这不会发生在后续迭代中,
i
将是
i=p+2
。第一个iter:
getDistance(中心列表[0],中心列表[0])
,第二个iter:
getDistance(中心列表[1],中心列表[2])
。。。为什么?从数学上讲,比较同一点的距离为零。如果x和y和距离x=y,那么从x到y的距离是零。是的,很好。这就是为什么我运行
如果距离<20和距离>0
@DaOnlyOwner不是一个复杂的问题,更多的是CPU运行时间。效果很好!如果使用这种方法,距离小于20,是否有办法从
中心列表中删除其中一个元组?探戈需要两个元组。一个坐标与一个坐标之间的距离可能小于20,与另一个坐标之间的距离可能大于20。@DaOnlyOwner这不是复杂的问题,更多的是CPU运行时的问题。非常好!如果使用这种方法,距离小于20,是否有办法从
中心列表中删除其中一个元组?探戈需要两个元组。一个坐标与一个坐标的距离可能小于20,与另一个坐标的距离可能大于20。