I have two sets of arrays, and what I am looking for is the index of the closest point in array2 to each value in array1, for example:
import numpy as np
from scipy.spatial import distance
array1 = np.array([[1,2,1], [4,2,6]])
array2 = np.array([[0,0,1], [4,5,0], [1,2,0], [6,5,0]])
def f(x):
return distance.cdist([x], array2 ).argmin()
def array_map(x):
return np.array(list(map(f, x)))
array_map(array1)
This code returns the correct results but is slow when both arrays are very big. I was wondering if it was possible to make this any quicker ?
asked Apr 12, 2019 at 13:07
-
2You should look into Quadtrees en.m.wikipedia.org/wiki/QuadtreeMax7cd– Max7cd2019年04月12日 13:10:12 +00:00Commented Apr 12, 2019 at 13:10
1 Answer 1
Thanks to @Max7CD here is a working solution that works quite efficiantly (at least for my purpose):
from scipy import spatial
tree =spatial.KDTree(array2)
slitArray = np.split(array1, 2) #I split the data so that the KDtree doesn't take for ever and so that I can moniter progress, probably useless
listFinal = []
for elem in slitArray:
a = tree.query(elem)
listFinal.append(a[1])
print("Fnished")
b = np.array(listFinal).ravel()
answered Apr 12, 2019 at 13:56
Sign up to request clarification or add additional context in comments.
Comments
lang-py