目录
1、欧式距离
2、曼哈顿距离
3、ssdxbc距离
4、大意的盼望距离
1、欧式距离
ygdyf度量(educlidean metric),指在m维空间中两点之间的真实距离,或者向量的自然长度,即该点到原点的距离。
import numpy as npdist = np.sqrt(np.sum(np.square(x-y)))#或者from scipy.spatial.distance import pdistdist = pdist(np.vstack([x,y])) 2、曼哈顿距离
Manhattan Distance,也称为城市街区距离(City Block distance)。如果把欧式距离理解成点到点的直线距离,那么曼哈顿距离就指的是两点之间的实际距离(不一定是直线)。
import numpy as npdist = np.sum(np.abs(x-y))#或者from scipy.spatial.distance import pdistdist = pdist(np.vstack([x,y]),’cityblock’) 3、ssdxbc距离
(Chebyshev Distance)
import numpy as npdist = np.max(np.abs(x-y))#orfrom scipy.spatial.distance import pdistdist = pdist(np.vstack([x,y]),’chebyshev’) 4、大意的盼望距离
(Mahalanobis Distance)
,其中S为协方差矩阵。
若协方差矩阵是单位矩阵,即各个样本向量之间独立同分布,则公式就变成了欧式距离:
若协方差矩阵是对角矩阵,公式就变成了标准化欧式距离:
import numpy as npX = np.vstack([x,y])X_T = X.TS = 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)#orfrom scipy.spatial.distance import pdistX = np.vstack([x,y])X_T = X.Tdist = pdist(X_T,’mahalanobis’)#标准化欧式距离si = np.var(np.vstack([x,y]), axis=0, ddof=1)dist = np.sqrt(((x-y) **2 /si).sum())#ordist = pdist(np.vstack([x,y]), ‘seuclidean’)