欧氏距离(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 np

def euclidean_distance(x, y):
# 方法一
# d = sqrt(sum(pow(a - b, 2) for a, b in zip(x, y)))

# 方法二
x, y = np.array(x), np.array(y)

# d = np.linalg.norm(x - y, ord=2)

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) # 11.180339887498949

from scipy.spatial.distance import euclidean
euclidean(x, y) # 11.180339887498949

标准化欧氏距离(Standardized Euclidean Distance)

将各个分量都“标准化”到均值、方差相等所得出的距离。

1
2
3
4
5
from scipy.spatial.distance import seuclidean
seuclidean([1, 0, 0], [0, 1, 0], [0.1, 0.1, 0.1]) # 4.4721359549995796
seuclidean([1, 0, 0], [0, 1, 0], [1, 0.1, 0.1]) # 3.3166247903553998
seuclidean([1, 0, 0], [0, 1, 0], [10, 0.1, 0.1]) # 3.1780497164141406

马氏距离(Mahalanobis Distance)

马哈拉诺比斯距离是由印度统计学家马哈拉诺比斯提出的,表示数据的协方差距离。它是一种有效的计算两个未知样本集的相似度的方法。

1
2
3
4
5
6
from scipy.spatial.distance import mahalanobis
iv = [[1, 0.5, 0.5], [0.5, 1, 0.5], [0.5, 0.5, 1]]
mahalanobis([1, 0, 0], [0, 1, 0], iv) # 1.0
mahalanobis([0, 2, 0], [0, 1, 0], iv) # 1.0
mahalanobis([2, 0, 0], [0, 1, 0], iv) # 1.7320508075688772

编辑距离

编辑距离是针对二个字符串的差异程度的量化量测,量测方式是看至少需要多少次的处理才能将一个字符串变成另一个字符串。

1
2
3
4
5
6
7
8
import Levenshtein

s1 = 'Hello, World!'
s2 = 'I am very happy!'

Levenshtein.distance(s1, s2) # 14
Levenshtein.ratio(s1, s2) # 0.20689655172413793

曼哈顿距离(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 np

def manhattan_distance(x, y):
# 方法一
# d = sum(abs(a - b) for a, b in zip(x, y))

# 方法二
x, y = np.array(x), np.array(y)

# d = np.linalg.norm(x - y, ord=1)

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) # 25

from scipy.spatial.distance import cityblock
cityblock(x, y) # 25

切比雪夫距离(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 np

def chebyshev_distance(x, y):
x, y = np.array(x), np.array(y)

# d = np.linalg.norm(x - y, ord=np.inf)

d = np.abs(x - y).max()

return d


x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]
chebyshev_distance(x, y) # 5

from scipy.spatial.distance import chebyshev
chebyshev(x, y) # 5

闵可夫斯基距离(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 Decimal

def 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) # Decimal('25.000')
minkowski_distance(x, y, 2) # Decimal('11.180')
minkowski_distance(x, y, 3) # Decimal('8.550')


import numpy as np

def minkowski_distance(x, y, ord=1):
# ord = 1 一范数
# ord = 2 二范数
# ord = np.inf 无穷范数
d = np.linalg.norm(x - y, ord=ord)
return d


from scipy.spatial.distance import minkowski
minkowski(x, y, p=1) # 25.0
minkowski(x, y, p=2) # 11.180339887498949
minkowski(x, y, p=3) # 8.549879733383484

汉明距离(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 np

def hamming_distance(x, y):
x, y = np.array(x), np.array(y)

# 方法一
# d = len(np.nonzero(x - y)[0])

# 方法二
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) # 5

from scipy.spatial.distance import hamming
hamming(x, y) # 1.0

余弦相似度(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) # 0.9649505047327671

from sklearn.metrics.pairwise import cosine_similarity
x, y = np.array(x), np.array(y)
cosine_similarity(x.reshape(1, -1), y.reshape(1, -1)) # array([[0.9649505]])

皮尔森相关系数(Pearson Correlation Coefficient)

用于度量两个变量X和Y之间的相关程度,其值介于-1与1之间。

1
2
3
4
5
from scipy.stats import pearsonr
x = [1, 2, 3, 4, 5]
y = [10, 9, 2.5, 6, 4]
coe, pv = pearsonr(x, y) # coe = -0.7426106572325057, pv = 0.1505558088534455

杰卡德相似系数(Jaccard Similarity Coefficient)及杰卡德距离(Jaccard Distance)

Jaccard 相似指数用来度量两个集合之间的相似性,它被定义为两个集合交集的元素个数除以并集的元素个数。

Jaccard 距离用来度量两个集合之间的差异性,它是 Jaccard 的相似系数的补集,被定义为 1 减去 Jaccard 相似系数。

1
2
3
4
5
6
from scipy.spatial.distance import jaccard

x = 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) # 0.75

评论