Search code examples
pythonscipy

Find the distance of each pair between two vectors


I have two vectors, let's say x=[2,4,6,7] and y=[2,6,7,8] and I want to find the euclidean distance, or any other implemented distance (from scipy for example), between each corresponding pair. That will be dist=[0, 2, 1, 1].

When I try

dist = scipy.spatial.distance.cdist(x,y, metric='sqeuclidean')

or

dist = [scipy.spatial.distance.cdist(x,y, metric='sqeuclidean') for x,y in zip(x,y)]

I get

ValueError: XA must be a 2-dimensional array.

How am I supposed to calculate dist and why do I have to reshape data for that purpose?


Solution

  • cdist does not compute the list of distances between corresponding pairs, but the matrix of distances between all pairs.

    np.linalg.norm((np.asarray(x)-np.asarray(y))[:, None], axis=1)
    

    Is how id typically write this for the Euclidian distance between n-dimensional points; but if you are only dealing with 1 dimensional points, the absolute difference, as suggested by elpres would be simpler.