calculate distance of 2 list of points in numpy

Using direct numpy broadcasting, you can do this:

dist = np.sqrt(((a[:, None] - b[:, :, None]) ** 2).sum(0))

Alternatively, scipy has a routine that will compute this slightly more efficiently (particularly for large matrices)

from scipy.spatial.distance import cdist
dist = cdist(a, b)

I would avoid solutions that depend on factoring-out matrix products (of the form A^2 + B^2 – 2AB), because they can be numerically unstable due to floating point roundoff errors.

Leave a Comment