Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python alternative for calculating pairwise distance between two sets of 2d points [duplicate]

In Matlab there exists the pdist2 command. Given the matrix mx2 and the matrix nx2, each row of matrices represents a 2d point. Now I want to create a mxn matrix such that (i,j) element represents the distance from ith point of mx2 matrix to jth point of nx2 matrix. I simply call the command pdist2(M,N).

I am looking for an alternative to this in python. I can of course write 2 for loops but since I am working with 2 numpy arrays, using for loops is not always the best choice. Is there an optimized command for this in the python universe? Basically I am asking for python alternative to MATLAB's pdist2.

like image 206
user_1_1_1 Avatar asked Apr 27 '17 07:04

user_1_1_1


1 Answers

You're looking for the cdist scipy function. It will calculate the pair-wise distances (euclidean by default) between two sets of n-dimensional matrices.

from scipy.spatial.distance import cdist
import numpy as np

X = np.arange(10).reshape(-1,2)
Y = np.arange(10).reshape(-1,2)

cdist(X, Y)
[[  0.           2.82842712   5.65685425   8.48528137  11.3137085 ]
 [  2.82842712   0.           2.82842712   5.65685425   8.48528137]
 [  5.65685425   2.82842712   0.           2.82842712   5.65685425]
 [  8.48528137   5.65685425   2.82842712   0.           2.82842712]
 [ 11.3137085    8.48528137   5.65685425   2.82842712   0.        ]]
like image 125
Duncan WP Avatar answered Oct 28 '22 21:10

Duncan WP