簡體   English   中英

如何從幾個numpy數組中找到兩個最接近的numpy數組?

[英]How to find the two closest numpy arrays out of several numpy arrays?

我有幾個numpy數組,我想比較它們並找到給定數組的最接近數組。 我可以使用https://docs.scipy.org/doc/scipy/reference/generation/scipy.spatial.distance.cdist.html計算這些數組之間的距離。 但是,有沒有辦法從幾個numpy數組中找到兩個最接近的數組?

對於我得到的數組print(arr.shape)給出(300,)

要找到最接近的兩個,您需要計算距離矩陣,然后在此矩陣中找到最小值以獲取彼此最接近的坐標(使用矩陣,您將獲得坐標的索引)。

from scipy.spatial import distance
import numpy as np 

coords = np.array([
  (35.0456, -85.2672),
  (35.1174, -89.9711),
  (35.9728, -83.9422),
  (36.1667, -86.7833)
])

distances = distance.cdist(coords, coords, 'euclidean')

# If you don't do that then the distance to self will always 
# be the min. distance in the matrix (always 0): 
np.fill_diagonal(distances, np.inf)

min_index = (np.argmin(distances))
closest = np.unravel_index(min_index, distances.shape)

一旦定義了closest索引,就可以獲取有關最接近的坐標對的所有信息:

print(f"The two closest are {closest}")
print(f"They are at distance {distances[closest]}")
print(f"Resp. coordinates {coords[closest[0]]} and {coords[closest[1]]}")

輸出:

The two closest are (0, 2)
They are at distance 1.6171965990565296
Resp. coordinates [ 35.0456 -85.2672] and [ 35.9728-83.9422]

最后,請注意所有這些輸入也將起作用:

coords = np.array([ [35.0456, -85.2672], [35.1174, -89.9711] ])

arr1 = [35.0456, -85.2672]
arr2 = [35.1174, -89.9711]
coords = np.array([arr1, arr2])

編寫距離函數然后使用itertools計算列表對之間的距離如何?

例如:

a_1 = [0,3,4,5]
a_2 = [4,7,8,9]
a_3 = [12, 34, 44]

from itertools import combinations

def distance(list1, list2):
    """Distance between two vectors."""
    squares = [(p-q) ** 2 for p, q in zip(list1, list2)]
    return sum(squares) ** .5

distances = []
for pair in combinations([a_1, a_2, a_3], 2):
    distances.append(pair)
    distances.append(distance(list(pair[0]), list(pair[1])))

結果:

 [([0, 3, 4, 5], [4, 7, 8, 9]), 8.0, ([0, 3, 4, 5], [12, 34, 44]), 52.009614495783374, ([4, 7, 8, 9], [12, 34, 44]), 45.70557952810576]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM