1
I have a array_objetivo
and would like to find the id and the distance (Euclidean in this example) of n
arrays closer to him that are in array_all
. Following are examples of arrays.
array_objetivo = np.array([2,2,3,4])
array_all = np.array([[1,1,2,2],
[2,2,3,3],
[3,3,4,4],
[4,4,5,5]])
To find only the nearest, I did:
def calcula_distancia(array1, array2):
# Distância Euclidiana
return np.linalg.norm(array1-array2)
def pega_vetor_mais_proximo(array_objetivo, array_all):
menor_dist = calcula_distancia(array_objetivo, array_all[0])
id_menor_dist = 0
for i in range(1, array_all.shape[0]):
dist = calcula_distancia(array_objetivo, array_all[i])
if dist < menor_dist:
menor_dist = dist
id_menor_dist = i
return menor_dist, id_menor_dist
How can I modify to find the n
nearest array efficiently?
If I understand correctly, this way I calculate the distance from the
array_objetivo
with all the lists ofarray_all
n times. And yet, when I remove a list fromarray_all
, ids change. My goal is to get the ids of the nearest lists.– AlexCiuffa
Really, if you want the ID was an error of interpretation my case need the values the described form meets. An elegant way and depending on the very heavy code would be to use the solution I proposed and make a go of it. index of each of the n arrays you took.
– Filipe Gonçalves