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

╄→гoц情女王★ 提交于 2019-12-01 06:00:58

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.        ]]

You should check the pairwise_distances method of the scikit-learn package.

sklearn.metrics.pairwise.pairwise_distances(X, Y=None, metric='euclidean', n_jobs=1, **kwds)

More information in http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html

If your matrix is not too big, this should do without using other libs. If the matrix is big, this method will be a bit slow and memory intensive.

mx2 = np.random.randint(1,9,5)    
nx2 = np.random.randint(1,9,3)    
mx2
Out[308]: array([2, 3, 4, 8, 7])    
nx2
Out[309]: array([3, 2, 2])    
mx2[:,None]-nx2
Out[310]: 
array([[-1,  0,  0],
       [ 0,  1,  1],
       [ 1,  2,  2],
       [ 5,  6,  6],
       [ 4,  5,  5]])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!