In Python, I have a vector v
of 300 elements and an array arr
of 20k 300-dimensional vectors. How do I get quickly the indices of the k
closest elements to v
from the array arr
?
CodePudding user response:
Since 300 is a very small number, sorting all elements and then just using the `k first is not an expensive operation (usually; it depends on how many thousand times per second you need to do this).
so, sorted()
is your friend; use the key=
keyword argument, sorted_vector = sorted(v ,key=…)
to implement sorting by euclidean distance.
Then, use the classic array[:end]
syntax to select the first k
.
CodePudding user response:
You can do this task with numpy
import numpy as np
v = np.array([[1,1,1,1]])
arr = np.array([
[1,1,1,1],
[2,2,2,2],
[3,3,3,3]
])
dist = np.linalg.norm(v - arr, axis=1) # Euclidean distance
min_distance_index = np.argmin(dist) # Find index of minimum distance
closest_vector = arr[min_distance_index] # Get vector having minimum distance
closest_vector
# array([1, 1, 1])