1

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
1

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.