欧氏距离(Euclidean Distance) 欧氏距离(也称欧几里得度量)指在 m 维空间中两个点之间的真实距离,或者向量的自然长度(即该点到原点的距离)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 from math import *import numpy as npdef euclidean_distance (x, y ): x, y = np.array(x), np.array(y) d = np.sqrt(np.sum (np.square(x - y))) return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] euclidean_distance(x, y) from scipy.spatial.distance import euclideaneuclidean(x, y)
标准化欧氏距离(Standardized Euclidean Distance) 将各个分量都“标准化”到均值、方差相等所得出的距离。
1 2 3 4 5 from scipy.spatial.distance import seuclideanseuclidean([1 , 0 , 0 ], [0 , 1 , 0 ], [0.1 , 0.1 , 0.1 ]) seuclidean([1 , 0 , 0 ], [0 , 1 , 0 ], [1 , 0.1 , 0.1 ]) seuclidean([1 , 0 , 0 ], [0 , 1 , 0 ], [10 , 0.1 , 0.1 ])
马氏距离(Mahalanobis Distance) 马哈拉诺比斯距离是由印度统计学家马哈拉诺比斯提出的,表示数据的协方差距离。它是一种有效的计算两个未知样本集的相似度的方法。
1 2 3 4 5 6 from scipy.spatial.distance import mahalanobisiv = [[1 , 0.5 , 0.5 ], [0.5 , 1 , 0.5 ], [0.5 , 0.5 , 1 ]] mahalanobis([1 , 0 , 0 ], [0 , 1 , 0 ], iv) mahalanobis([0 , 2 , 0 ], [0 , 1 , 0 ], iv) mahalanobis([2 , 0 , 0 ], [0 , 1 , 0 ], iv)
编辑距离 编辑距离是针对二个字符串的差异程度的量化量测,量测方式是看至少需要多少次的处理才能将一个字符串变成另一个字符串。
1 2 3 4 5 6 7 8 import Levenshteins1 = 'Hello, World!' s2 = 'I am very happy!' Levenshtein.distance(s1, s2) Levenshtein.ratio(s1, s2)
曼哈顿距离(Manhattan Distance) 在欧几里得空间的固定直角坐标系上两点所形成的线段对轴产生的投影的距离总和。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 from math import *import numpy as npdef manhattan_distance (x, y ): x, y = np.array(x), np.array(y) d = np.sum (np.abs (x - y)) return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] manhattan_distance(x, y) from scipy.spatial.distance import cityblockcityblock(x, y)
切比雪夫距离(Chebyshev Distance) 数学上,切比雪夫距离或是L∞度量是向量空间中的一种度量,二个点之间的距离定义为其各座标数值差的最大值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import numpy as npdef chebyshev_distance (x, y ): x, y = np.array(x), np.array(y) d = np.abs (x - y).max () return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] chebyshev_distance(x, y) from scipy.spatial.distance import chebyshevchebyshev(x, y)
闵可夫斯基距离(Minkowski Distance) 明氏距离又叫做明可夫斯基距离,是欧氏空间中的一种测度,被看做是欧氏距离和曼哈顿距离的一种推广。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from math import *from decimal import Decimaldef nth_root (value, n_root ): root_value = 1 / float (n_root) return round (Decimal(value)**Decimal(root_value), 3 ) def minkowski_distance (x, y, p_value ): d = nth_root(sum (pow (abs (a - b), p_value) for a, b in zip (x, y)), p_value) return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] minkowski_distance(x, y, 1 ) minkowski_distance(x, y, 2 ) minkowski_distance(x, y, 3 ) import numpy as npdef minkowski_distance (x, y, ord =1 ): d = np.linalg.norm(x - y, ord =ord ) return d from scipy.spatial.distance import minkowskiminkowski(x, y, p=1 ) minkowski(x, y, p=2 ) minkowski(x, y, p=3 )
汉明距离(Hamming Distance) 在信息论中,两个等长字符串之间的汉明距离是两个字符串对应位置的不同字符的个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import numpy as npdef hamming_distance (x, y ): x, y = np.array(x), np.array(y) d = np.shape(np.nonzero(x - y)[0 ])[0 ] return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] hamming_distance(x, y) from scipy.spatial.distance import hamminghamming(x, y)
余弦相似度(Cosine Similarity) 余弦相似性通过测量两个向量的夹角的余弦值来度量它们之间的相似性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 def cosine_distance (x, y ): x, y = np.array(x), np.array(y) d = np.dot(x, y) / (np.linalg.norm(x) * np.linalg.norm(y)) return d x = [1 , 2 , 3 , 4 , 5 ] y = [6 , 7 , 8 , 9 , 10 ] cosine_distance(x, y) from sklearn.metrics.pairwise import cosine_similarityx, y = np.array(x), np.array(y) cosine_similarity(x.reshape(1 , -1 ), y.reshape(1 , -1 ))
皮尔森相关系数(Pearson Correlation Coefficient) 用于度量两个变量X和Y之间的相关程度,其值介于-1与1之间。
1 2 3 4 5 from scipy.stats import pearsonrx = [1 , 2 , 3 , 4 , 5 ] y = [10 , 9 , 2.5 , 6 , 4 ] coe, pv = pearsonr(x, y)
杰卡德相似系数(Jaccard Similarity Coefficient)及杰卡德距离(Jaccard Distance) Jaccard 相似指数用来度量两个集合之间的相似性,它被定义为两个集合交集的元素个数除以并集的元素个数。
Jaccard 距离用来度量两个集合之间的差异性,它是 Jaccard 的相似系数的补集,被定义为 1 减去 Jaccard 相似系数。
1 2 3 4 5 6 from scipy.spatial.distance import jaccardx = np.array([1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 ]) y = np.array([0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 ]) jaccard(x, y)