下面的代码查找列表a的每个元素与列表b的每个元素之间的欧几里得距离。
from scipy.spatial import distance
a = [[1, 2, 3], [4, 5, 6]]
b = [[10, 20]]
Final_distance = []
for i in [j for sub in a for j in sub]:
for k in [m for t in b for m in t]:
dist = distance.euclidean(i, k)
Final_distance.append(dist)
print(Final_distance)
输出为
[9.0, 19.0, 8.0, 18.0, 7.0, 17.0, 6.0, 16.0, 5.0, 15.0, 4.0, 14.0]
但是对于很大的列表,它需要很长时间。有没有办法减少上述代码的时间复杂度?
答案 0 :(得分:2)
由于您的欧式距离是标量,所以它等于每个点之间的绝对值。因此,您可以使用np.repeat
和np.tile
以适当的顺序重复数组,而只需将数组彼此相减:
import numpy as np
a = [[1, 2, 3], [4, 5, 6]]
b = [[10, 20]]
a1 = np.array(a).flatten()
b1 = np.array(b).flatten()
Final_distance = np.abs(np.subtract(np.repeat(a1, len(b1)), np.tile(b1, len(a1))))
哪个返回:
array([ 9, 19, 8, 18, 7, 17, 6, 16, 5, 15, 4, 14])