Difference between scipy pairwise distance and X.X+Y.Y - X.Y^t

谁都会走 提交于 2019-12-02 13:11:46

问题


Let's imagine we have data as

d1 = np.random.uniform(low=0, high=2, size=(3,2))
d2 = np.random.uniform(low=3, high=5, size=(3,2))
X = np.vstack((d1,d2))

X
array([[ 1.4930674 ,  1.64890721],
       [ 0.40456265,  0.62262546],
       [ 0.86893397,  1.3590808 ],
       [ 4.04177045,  4.40938126],
       [ 3.01396153,  4.60005842],
       [ 3.2144552 ,  4.65539323]])

I want to compare two methods for generating the pairwise distances:

assuming that X and Y are the same:

(X-Y)^2 = X.X + Y.Y - 2*X.Y^t

Here is the first method as it is used in scikit-learn for computing the pairwise distance, and later for kernel matrix.

import numpy as np
def cal_pdist1(X):
       Y = X
       XX = np.einsum('ij,ij->i', X, X)[np.newaxis, :]
       YY = XX.T
       distances = -2*np.dot(X, Y.T)
       distances += XX
       distances += YY
       return(distances)

cal_pdist1(X)
array([[  0.        ,   2.2380968 ,   0.47354188,  14.11610424,
         11.02241244,  12.00213414],
       [  2.2380968 ,   0.        ,   0.75800718,  27.56880003,
         22.62893544,  24.15871196],
       [  0.47354188,   0.75800718,   0.        ,  19.37122424,
         15.1050792 ,  16.36714548],
       [ 14.11610424,  27.56880003,  19.37122424,   0.        ,
          1.09274896,   0.74497242],
       [ 11.02241244,  22.62893544,  15.1050792 ,   1.09274896,
          0.        ,   0.04325965],
       [ 12.00213414,  24.15871196,  16.36714548,   0.74497242,
          0.04325965,   0.        ]])

Now, if I use scipy pairwise distance function as below, I get

import scipy, scipy.spatial
pd_sparse = scipy.spatial.distance.pdist(X, metric='seuclidean')
scipy.spatial.distance.squareform(pd_sparse)
array([[ 0.        ,  0.92916653,  0.45646989,  2.29444795,  1.89740167,
         2.00059442],
       [ 0.92916653,  0.        ,  0.50798432,  3.22211357,  2.78788236,
         2.90062103],
       [ 0.45646989,  0.50798432,  0.        ,  2.72720831,  2.28001564,
         2.39338343],
       [ 2.29444795,  3.22211357,  2.72720831,  0.        ,  0.71411943,
         0.58399694],
       [ 1.89740167,  2.78788236,  2.28001564,  0.71411943,  0.        ,
         0.14102567],
       [ 2.00059442,  2.90062103,  2.39338343,  0.58399694,  0.14102567,
         0.        ]])

The results are completely different! Shouldn't they be the same?


回答1:


pdist(..., metric='seuclidean') computes the standardized Euclidean distance, not the squared Euclidean distance (which is what cal_pdist returns).

From the docs:

Y = pdist(X, 'seuclidean', V=None)

Computes the standardized Euclidean distance. The standardized Euclidean distance between two n-vectors u and v is

   __________________
  √∑(ui−vi)^2 / V[xi]

V is the variance vector; V[i] is the variance computed over all the i’th components of the points. If not passed, it is automatically computed.

Try passing metric='sqeuclidean', and you will see that both functions return the same result to within rounding error.



来源:https://stackoverflow.com/questions/31734868/difference-between-scipy-pairwise-distance-and-x-xy-y-x-yt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!