python - 使用 numpy/scipy 计算连续向量之间距离的最快方法

标签 python python-2.7 numpy distance insertion

我有一个线增长算法,我需要:

  • 计算数组中连续向量之间的距离(欧氏距离)
  • 在距离大于特定阈值的位置插入新向量

enter image description here

我通常以非常幼稚的方式执行此操作(请参阅下面的代码),并且想知道如何使用 numpy 以最快的方式计算连续向量之间的距离(如果需要,还可以使用 scipy)。

import math


threshold = 10
vectorList = [(0, 10), (4, 8), (14, 14), (16, 19), (35, 16)]

for i in xrange(len(vectorList)):
    p1 = vectorList[i]
    p2 = vectorList[i+1]
    d = math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
    if d >= threshold:
        pmid = ((p1[0] + p2[0]) * .5, (p1[1] + p2[1]) * .5)
        vectorList.insert(i+1, pmid)

编辑: 我想出了以下解决方法,但我仍然担心距离计算。

我需要计算一个向量与其列表中的下一个邻居之间的距离,而不是像我在这里所做的那样计算整个距离矩阵(所有向量彼此相对)。

import numpy as np

vectorList = [(0, 10), (4, 8), (14, 14), (16, 19), (35, 16)]
arr = np.asarray(vectorList).astype(float)


dis = distance.cdist(arr, arr).diagonal(1)
idx = np.where(dis > 10)[0]
vec = (arr[idx] + arr[idx+1]) * .5
arr = np.insert(arr, idx+1, vec, 0)

# output
array([[ 0. , 10. ],[ 4. ,  8. ],[ 9. , 11. ],[14. , 14. ],[16. , 19. ],[25.5, 17.5],[35. , 16. ]])

最佳答案

In [209]: vectorList = [(0, 10), (4, 8), (14, 14), (16, 19), (35, 16), (39,50)] 
In [210]: vectorList                                                            
Out[210]: [(0, 10), (4, 8), (14, 14), (16, 19), (35, 16), (39, 50)]

我添加了一个点,形成 3 个可能的插入点。

In [212]: np.diff(vectorList, axis=0)                                           
Out[212]: 
array([[ 4, -2],
       [10,  6],
       [ 2,  5],
       [19, -3],
       [ 4, 34]])
In [213]: np.sum(np.diff(vectorList, axis=0)**2,1)                              
Out[213]: array([  20,  136,   29,  370, 1172])

距离:

In [214]: np.sqrt(np.sum(np.diff(vectorList, axis=0)**2,1))                     
Out[214]: array([ 4.47213595, 11.66190379,  5.38516481, 19.23538406, 34.23448554])

平均值:

In [216]: arr = np.array(vectorList)                                            
In [217]: arr                                                                   
Out[217]: 
array([[ 0, 10],
       [ 4,  8],
       [14, 14],
       [16, 19],
       [35, 16],
       [39, 50]])

In [218]: (arr[:-1]+arr[1:])/2                                                  
Out[218]: 
array([[ 2. ,  9. ],
       [ 9. , 11. ],
       [15. , 16.5],
       [25.5, 17.5],
       [37. , 33. ]])

我可以在没有 diff 的情况下做类似的事情:

d = np.sqrt(np.sum((arr[1:]-arr[:-1])**2,1)) 

超过阈值的跳跃:

In [224]: idx = np.nonzero(d>10)                                                
In [225]: idx                                                                   
Out[225]: (array([1, 3, 4]),)
In [227]: _218[idx]       # the mean values to insert                                                            
Out[227]: 
array([[ 9. , 11. ],
       [25.5, 17.5],
       [37. , 33. ]])

使用np.insert插入所有值。

In [232]: np.insert(arr.astype(float), idx[0]+1, _227, axis=0)                  
Out[232]: 
array([[ 0. , 10. ],
       [ 4. ,  8. ],
       [ 9. , 11. ],
       [14. , 14. ],
       [16. , 19. ],
       [25.5, 17.5],
       [35. , 16. ],
       [37. , 33. ],
       [39. , 50. ]])

关于python - 使用 numpy/scipy 计算连续向量之间距离的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495397/

相关文章:

python - Pandas - 从字典列表创建 df

python - 有没有比这更好的方法来编写 "depend on parameters"的 Python 函数?

python - 从 "List"创建子列表

python - 如何在原始源代码中添加自定义NumPy函数?

python - np.where 和屏蔽数组

python - Tensorflow:从输入到输出的梯度计算

python - Google Cloud OAuth 同意屏幕未反射(reflect)(内部)应用程序范围的更改

python - 用 Python 代码编写的 SHA 512 crypt 输出与 mkpasswd 不同

python-2.7 - PyAIML 未加载启动

python - 如何根据 True 和 False 语句比较两个 numpy 数组