Find arrays with the shortest distance

Asked

Viewed 143 times

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?

2 answers

0

I can’t type a code in here the way I’d like but I think I can help you.

You create an empty list and a while loop based on a counter that will receive the amount of distances you want to return, inside that while you put your loop for the way it is in the code and at the end of that go you decrement your counter and delete from the list the distance you found, if your array_all is a list that cannot be modified you simply create a local variable that receives array_all and do what you were told.

  • If I understand correctly, this way I calculate the distance from the array_objetivo with all the lists of array_all n times. And yet, when I remove a list from array_all, ids change. My goal is to get the ids of the nearest lists.

  • 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.

0

I was able to solve by calculating all distances and storing them. Then, I get the ids of the lowest values from the distance list and the returns.

def pega_n_vetores_mais_proximos(array_objetivo, array_all, n):
    distancias = []

    # calcula todas as distâncias e as armazena
    for i in range(0, array_all.shape[0]):
        distancias.append(calcula_distancia(array_objetivo,array_all[i]))

    distancias = np.array(distancias)

    # pega os ids das menores distâncias
    menores_n_idx = np.argsort(distancias)[:n]

    return distancias[menores_n_idx], menores_n_idx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.