3

I have two lists list1 and list2, each of size 5000 with every entry of the lists being a numpy.array. I want to calculate the squares of the Euclidean distances between the elements of the lists in a fast and efficient way, i.e. I need to calculate sum((list1[i]-list2[j])**2) for every combination of i and j, which are thus in total 2,500,000 combinations. I currently did so by running a double loop and writing every result into a 2d numpy.array by means of

result[i,j] = sum((list1[i]-list2[j])**2) 

but still need on my computer about 4 minutes of time. I was wondering, whether any tricks could be used to further speed the calculation up.

5
  • 3
    distance of i vs j is the same as distance of j vs i. You can cut half the time by using an inner loop that starts at the outer index Commented May 26, 2020 at 19:25
  • 4
    Why do you have lists of arrays? This would go much faster with 2D arrays and something like scipy.spatial.distance.cdist. Commented May 26, 2020 at 19:27
  • 3
    from scipy.spatial import distance_matrix; dist_mat = distance_matrix(list1, list2)? Commented May 26, 2020 at 19:27
  • Are the component arrays all the same shape? All 1d? Commented May 26, 2020 at 19:52
  • If all arrays in each list is the same shape, then a simple broadcasting ought to do the trick. Commented May 26, 2020 at 20:06

1 Answer 1

1

If you insist on numpy (assuming your inner arrays are 1-D):

dist_mat = ((list1[:,None,:]-list2[:,:])**2).sum(2)

Note that according to your definition of distance in question, this is square of Euclidean distances. If you want the distance itself, simply take square root of this.
Otherwise, I would prefer @Quang's comment:

from scipy.spatial import distance_matrix
dist_mat = distance_matrix(list1, list2)
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks, these are very good responses from everyone! Especially the scipy funtions speed up the computation of the distance matrix by ages.

Your Answer

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.