Answer the question
In order to leave comments, you need to log in
NumPy how to rewrite algorithm?
At the input, one numpy array and also a lot of arrays with which it needs to be compared and give out those that are the most "similar". It is done like this:
input_array= np.array(....)
many_arrays=[np.array(....),np.array(....), .... ]
dists = np.linalg.norm(many_arrays-input_array, axis=1)
ids = np.argsort(dists)[:20] #получаем первые 20 индексов максимально "похожих" на input_array
Answer the question
In order to leave comments, you need to log in
Well, in a couple or not - consider for yourself:
import itertools
import numpy as np
many_arrays=[np.array([1,2,3]),np.array([4,5,6]), np.array([7,8,9]) ]
many_arrays2=[np.array([1,2,3]),np.array([4,5,6]), np.array([7,8,9]) ]
prd=itertools.product(many_arrays,many_arrays2)
dists=[]
for it in prd:
dists.append([np.linalg.norm(it[0]-it[1]),it[0],it[1]])
sorted(dists, key=lambda x: x[0])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question