常用的向量距离公式

目录

 

1、欧式距离

2、曼哈顿距离

3、切比雪夫距离

4、马氏距离


1、欧式距离

欧几里得度量(educlidean metric),指在m维空间中两点之间的真实距离,或者向量的自然长度,即该点到原点的距离。

d(x,y)=\sqrt{\sum _{i=1} ^{n} (x_{i}-y_{i})^2}

import numpy as np
dist = np.sqrt(np.sum(np.square(x-y)))

#或者
from scipy.spatial.distance import pdist
dist = pdist(np.vstack([x,y]))

2、曼哈顿距离

Manhattan Distance,也称为城市街区距离(City Block distance)。如果把欧式距离理解成点到点的直线距离,那么曼哈顿距离就指的是两点之间的实际距离(不一定是直线)。

d(x,y) = \sum_{i=1}^{n} |x_i - y_i|

import numpy as np
dist = np.sum(np.abs(x-y))

#或者
from scipy.spatial.distance import pdist
dist = pdist(np.vstack([x,y]),'cityblock')

3、切比雪夫距离

(Chebyshev Distance)

d(x,y) = \lim_{i\rightarrow \infty } (\sum_{i=1}^{n} |x_i - y_i|)^{\frac{1}{i}}

import numpy as np
dist = np.max(np.abs(x-y))

#or
from scipy.spatial.distance import pdist
dist = pdist(np.vstack([x,y]),'chebyshev')

4、马氏距离

(Mahalanobis Distance)

d(x,y)=\sqrt{(x_i-y_i)^T S^{-1}(x_i - y_i)},其中S为协方差矩阵。

若协方差矩阵是单位矩阵,即各个样本向量之间独立同分布,则公式就变成了欧式距离:

d(x,y) = \sqrt{(x_i - y_i)^T (x_i - y_i)}

若协方差矩阵是对角矩阵,公式就变成了标准化欧式距离:

d(x,y) = \sqrt{\sum_{i=1}^{n}(\frac{x_i - y_i}{S_i})^2}

import numpy as np

X = np.vstack([x,y])
X_T = X.T
S = np.cov(X)#两个维度之间协方差矩阵
SI = np.linalg.inv(S)#协方差矩阵的逆矩阵
n = XT.shape[0]#样本之间两两组合
dist = []
for i in range(0,n):
    for j in range(i+1,n):
        delta = X_T[i] - X_T[j]
        d = np.sqrt(np.dot(np.dot(delta,SI), delta.T))
        dist.append(d)

#or
from scipy.spatial.distance import pdist
X = np.vstack([x,y])
X_T = X.T
dist = pdist(X_T,'mahalanobis')


#标准化欧式距离

si = np.var(np.vstack([x,y]), axis=0, ddof=1)
dist = np.sqrt(((x-y) **2 /si).sum())
#or
dist = pdist(np.vstack([x,y]), 'seuclidean')

 

你可能感兴趣的:(python)