【機器學習】5種距離度量方法詳解+Python實現([]+lambda+np.frompyfunc+向量法等多種方法實現)
介紹的五種距離度量方法是:歐氏距離(Euclidean Distance),曼哈頓距離(Manhattan Distance),夾角餘弦(Angle Cosine),切比雪夫距離(Chebyshev Distance),漢明距離(Hamming Distance)。
1.歐式距離(Euclidean Distance)
1.1數學公式
Euclidean Distance,即兩點之間的直線距離,數學公式如下所示。
(1)二維平面上兩點a(x1,y1),b(x2,y2)之間的歐式距離公式
(2)n維空間上兩點a(x1,x2……..xn),b(y1,y2……..yn)的歐式距離公式
1.2Python實現
歐氏距離的實現用了三種方法:
1)lambda+[]
2)function + np.frompyfunc
3)lambda + np.frompyfunc
lambda + np.frompyfunc是最舒服的一種實現(多個)序列之間相互操作的方法。
# -*- coding: utf-8 -*- """ @author: 蔚藍的天空tom Aim:五種度量距離方法實現 """ import numpy as np def EM1(p):#數學公式法 '''EuclideanMetric, 歐幾里得距離, 也稱歐氏距離''' y = lambda a,b : np.power(a-b,2) s = sum([y(a,b) for (a,b) in zip(p[0], p[1])]) em = np.sqrt(s) return em def e_func(a,b): '''求兩個數的差的平方''' return np.power(a-b,2) def EM2(P):#function + np.frompyfunc方法 '''EuclideanMetric, 歐幾里得距離, 也稱歐氏距離''' func = np.frompyfunc(e_func, 2, 1) s = sum(func(P[0],P[1])) em = np.sqrt(s) return em def EM3(P):#lambda + np.frompyfunc方法 e_func = lambda a,b : np.power(a-b,2) func = np.frompyfunc(e_func, 2, 1) s = sum(func(P[0],P[1])) em = np.sqrt(s) return em def EM4(P):#向量法 P = np.array(P) K = P[0] - P[1] em = np.sqrt(sum(K * K.T))#向量乘 return em if __name__=='__main__': #二維空間,(1,2), (3,4) p = [[1,2], #p1 = (1,2) [3,4]] #p2 = (3,4) print('em:', EM1(p), EM2(p),EM3(p), EM4(p)) #4維空間,(1,2,3,4), (5,6,7,8) p = [[1,2,3,4], #p1 = (1,2,3,4) [5,6,7,8]] #p2 = (5,6,7,8) print('em:', EM1(p), EM2(p), EM3(p), EM4(p))
執行結果:
runfile('C:/Users/tom/5_distance.py', wdir='C:/Users/tom')
em: 2.82842712475 2.82842712475 2.82842712475 2.82842712475
em: 8.0 8.0 8.0 8.0
2.曼哈頓距離(Manhattan Distance)
2.1數學公式
ManhattanDistance,就是相同數軸座標差的絕對值的和
(1)二維平面上兩點a(x1,y1),b(x2,y2)之間的曼哈頓距離
(2)n維空間上兩點a(x1,x2……..xn),b(y1,y2……..yn)的曼哈頓距離公式:
2.2Python實現
# -*- coding: utf-8 -*- """ @author: 蔚藍的天空tom Aim:用盡可能多的方法實現曼哈頓距離Manhattan Distance """ import numpy as np def MD1(P):#數學公式法 y = lambda a,b : np.abs(a-b) md = sum([y(a,b) for (a,b) in zip(P[0], P[1])]) return md def e_func(a,b): return np.abs(a-b) def MD2(P):#function + np.frompyfunc方法 func = np.frompyfunc(e_func, 2, 1) md = sum(func(P[0], P[1])) return md def MD3(P):#lambda + np.frompyfunc方法 e_func = lambda a,b : np.abs(a-b) func = np.frompyfunc(e_func, 2, 1) md = sum(func(P[0], P[1])) return md def MD4(P):# e_func = lambda x: np.abs(x) func = np.frompyfunc(e_func, 1, 1) #md = sum(func(P[0] - P[1])) #error:list不支援-運算子
P = np.array(P)
md = sum(func(P[0] - P[1]))#np.ndarray向量減 return mdif __name__=='__main__': #二維空間,(1,2), (3,4) P = [[1,2], #p1 = (1,2) [3,4]] #p2 = (3,4) print('md:', MD1(P), MD2(P), MD3(P), MD4(P)) #n維空間,(1,2,3,4), (5,6,7,8) P = [[1,2,3,4], #p1 = (1,2,3,4) [5,6,7,8]] #p2 = (5,6,7,8) print('md:', MD1(P), MD2(P), MD3(P), MD4(P)) 執行結果:
runfile('C:/Users/tom/Manhattan_Distance.py', wdir='C:/Users/tom')
md: 4 4 4 4
md: 16 16 16 16
3.夾角餘弦
3.1數學公式
(1)二維平面上兩向量a(x1,y1),b(x2,y2)之間的夾角餘弦公式
也可以使用向量方法計算:
(2)n維空間上兩點a(x1,x2……..xn),b(y1,y2……..yn)的夾角餘弦公式
3.2Python實現
# -*- coding: utf-8 -*-
"""
@author: 蔚藍的天空tom
Aim:用盡可能多的方法實現夾角餘弦angle cosine
"""
import numpy as np
def AC1(P):
'''數學公式法'''
#分子
numerator = sum([(a*b) for (a,b) in zip(P[0], P[1])])
#分母
sq1 = np.sqrt(sum([np.power(e,2) for e in P[0]]))
sq2 = np.sqrt(sum([np.power(e,2) for e in P[1]]))
denominator = sq1 * sq2
#夾角餘弦公式
ac = numerator * 1.0/denominator
return ac
def e_func(a,b):
'''實現a*b,如果計算a*a,可以傳入a,a'''
return a*b
def AC2(P):
'''function + np.frompyfunc方法'''
func = np.frompyfunc(e_func, 2, 1)
#分子
numerator = sum(func(P[0], P[1]))
#分母
denminator = np.sqrt(sum(func(P[0], P[0]))) * \
np.sqrt(sum(func(P[1], P[1])))
#夾角餘弦
ac = numerator * 1.0/denminator
return ac
def AC3(P):
'''lambda + np.frompyfunc方法'''
e_func = lambda a,b : a*b
func = np.frompyfunc(e_func, 2, 1)
numerator = sum(func(P[0], P[1]))
denminator = np.sqrt(sum(func(P[0], P[0]))) *\
np.sqrt(sum(func(P[1], P[1])))
ac = numerator * 1.0/denminator
return ac
def AC4(P):
'''向量法'''
#numberator = sum(P[0]*P[1].T)#error:'list' object has no attribute 'T'
P = np.array(P) #先轉化為np.ndarray
numberator = sum(P[0]*P[1].T)#向量乘
denminator = np.sqrt(sum(P[0]*P[0].T)) *\
np.sqrt(sum(P[1]*P[1].T))
ac = numberator * 1.0/denminator
return ac
if __name__=='__main__':
#二維空間,(1,2), (3,4)
P = [[1,2], #p1 = (1,2)
[3,4]] #p2 = (3,4)
print('ac:', AC1(P), AC2(P), AC3(P), AC4(P))
#n維空間,(1,2,3,4), (5,6,7,8)
P = [[1,2,3,4], #p1 = (1,2,3,4)
[5,6,7,8]] #p2 = (5,6,7,8)
print('ac:', AC1(P), AC2(P), AC3(P), AC4(P))
執行結果:
runfile('C:/Users/tom/angle_cosine.py', wdir='C:/Users/tom')
ac: 0.9838699101 0.9838699101 0.9838699101 0.9838699101
ac: 0.968863931627 0.968863931627 0.968863931627 0.968863931627
4.切比雪夫距離(Chebyshev Distance)
4.1數學公式
(1)二維平面上兩點a(x1,y1),b(x2,y2)之間的切比雪夫距離公式
(2)n維空間上兩點a(x1,x2……..xn),b(y1,y2……..yn)的切比雪夫距離公式
4.2Python實現
# -*- coding: utf-8 -*-
"""
@author: 蔚藍的天空tom
Aim:用盡可能多的方法實現切比雪夫距離(Chebyshev Distance)
"""
import numpy as np
def CD1(P):
'''數學公式法'''
y = [np.abs(a-b) for (a,b) in zip(P[0],P[1])]
cd = max(y)
return cd
def e_func(a,b):
return np.abs(a-b)
def CD2(P):
'''function + np.frompyfunc方法'''
func = np.frompyfunc(e_func, 2, 1)
cd = max(func(P[0], P[1]))
return cd
def CD3(P):
'''lambda + np.frompyfunc'''
e_func = lambda a,b : np.abs(a-b)
func = np.frompyfunc(e_func, 2, 1)
cd = max(func(P[0], P[1]))
return cd
def CD4(P):
'''向量法'''
P = np.array(P)
cd = max(np.abs(P[0] - P[1])) #向量減和元素取絕對值
return cd
if __name__=='__main__':
#二維空間,(1,2), (3,4)
P = [[1,2], #p1 = (1,2)
[3,5]] #p2 = (3,5)
print('cd:', CD1(P), CD2(P), CD3(P), CD4(P))
#n維空間,(1,2,3,4), (5,7,9,11)
P = [[1,2,3,4], #p1 = (1,2,3,4)
[5,7,9,11]] #p2 = (5,7,9,11)
print('cd:', CD1(P), CD2(P), CD3(P), CD4(P))
執行結果:
runfile('C:/Users/tom/cd.py', wdir='C:/Users/tom')
cd: 3 3 3 3
cd: 7 7 7 7
5.漢明距離(Hamming Distance)
5.1數學公式
概念:漢明距離(Hamming Distance)就是以理查德.衛斯里.漢明的名字命名的。在資訊理論中,兩個等長字串之間的漢明距離就是兩個字串對應位置的不同字元的個數。換句話說,它就是將一個字串變換成另外一個字串所需要替換的字元個數。1001101與1011001之間的漢明距離就是2
95271314與95371234之間的漢明距離就是3
hello與Xello之間的漢明距離就是1
5.2Python實現
# -*- coding: utf-8 -*-
"""
@author: 蔚藍的天空tom
Aim:儘量多的方法實現漢明距離(Hamming Distance)
"""
import numpy as np
def HD1(S):
'''數學公式法'''
diff = lambda a,b : (a != b and [1] or [0])[0]
d = [diff(a,b) for (a,b) in zip(S[0], S[1])]
hd = d.count(1)
return hd
def e_func(a,b):
return ((a!=b) and [1] or [0])[0]
def HD2(S):
'''function + np.frompyfunc方法'''
func = np.frompyfunc(e_func, 2, 1)
y = func(list(S[0]), list(S[1]))
hd = list(y).count(1)
return hd
def HD3(S):
'''lambda + np.frompyfunc方法'''
e_func = lambda a,b : (a!=b and [1] or [0])[0]
func = np.frompyfunc(e_func, 2, 1)
y = func(list(S[0]), list(S[1]))
hd = list(y).count(1)
return hd
def HD4(S):
'''向量法,先實現了數字字串'''
s1 = np.array(list(S[0]), dtype=int)#向量法的必要條件:str--->list--->array
s2 = np.array(list(S[1]), dtype=int)
d = list(s1 - s2) #1)向量法,2)使用list的count(e)方法,所以array--->list
hd = d.__len__() - d.count(0) #list的元素總數方法__len__()
return hd
if __name__=='__main__':
#兩個字串'1001101'與'1011001'
S = ['1001101', '1011001']
print('hd:', HD1(S), HD2(S), HD3(S), HD4(S))
#兩個字串'95271314'與'95371234'
S = ['95271314', '95371234']
print('hd:', HD1(S), HD2(S), HD3(S), HD4(S))
#'ideal'與‘dream’
S = ['ideal', 'ideal']
print('hd:', HD1(S), HD2(S))
#‘study’與‘Student’
S = ['study', 'Student']
print('hd:', HD1(S))
執行結果
runfile('C:/Users/l13277/hd.py', wdir='C:/Users/l13277')
hd: 2 2 2 2
hd: 3 3 3 3
hd: 0 0
hd: 2
(end)