机器学习各种相似性度量及Python实现

转自:https://blog.csdn.net/u010412858/article/details/60467382

在做很多研究问题时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance)。采用什么样的方法计算距离是很讲究,甚至关系到分类的正确与否。

 

1、欧式距离

# 1) given two data points, calculate the euclidean distance between them
def get_distance(data1, data2):
points = zip(data1, data2)
diffs_squared_distance = [pow(a - b, 2) for (a, b) in points]
return math.sqrt(sum(diffs_squared_distance))

 

2、余弦相似度

def cosin_distance(vector1, vector2):
dot_product = 0.0
normA = 0.0
normB = 0.0
for a, b in zip(vector1, vector2):
dot_product += a * b
normA += a ** 2
normB += b ** 2
if normA == 0.0 or normB == 0.0:
return None
else:
return dot_product / ((normA * normB) ** 0.5)

 

3、用Numpy进行余弦相似度计算

sim = user_item_matric.dot(user_item_matric.T)
norms = np.array([np.sqrt(np.diagonal(sim))])
user_similarity=(sim / norms / norms.T)

 

4、用scikit cosine_similarity计算相似度

from sklearn.metrics.pairwise import cosine_similarity
user_similarity=cosine_similarity(user_tag_matric)

 

5、用scikit pairwise_distances计算相似度

from sklearn.metrics.pairwise import pairwise_distances
user_similarity = pairwise_distances(user_tag_matric, metric='cosine')

 

需要注意的一点是,用pairwise_distances计算的Cosine distance是1-(cosine similarity)结果

 

6. 曼哈顿距离

def Manhattan(vec1, vec2):
npvec1, npvec2 = np.array(vec1), np.array(vec2)
return np.abs(npvec1-npvec2).sum()
# Manhattan_Distance,

 

7. 切比雪夫距离

def Chebyshev(vec1, vec2):
npvec1, npvec2 = np.array(vec1), np.array(vec2)
return max(np.abs(npvec1-npvec2))
# Chebyshev_Distance

 

8. 闵可夫斯基距离

#!/usr/bin/env python

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):
return nth_root(sum(pow(abs(a-b),p_value) for a,b in zip(x,y)),p_value)

print(minkowski_distance([0,3,4,5],[7,6,3,-1],3))

 

9. 标准化欧氏距离

def Standardized_Euclidean(vec1,vec2,v):
from scipy import spatial
npvec = np.array([np.array(vec1), np.array(vec2)])
return spatial.distance.pdist(npvec, 'seuclidean', V=None)
# Standardized Euclidean distance
# http://blog.csdn.net/jinzhichaoshuiping/article/details/51019473

 

10. 马氏距离

def Mahalanobis(vec1, vec2):
npvec1, npvec2 = np.array(vec1), np.array(vec2)
npvec = np.array([npvec1, npvec2])
sub = npvec.T[0]-npvec.T[1]
inv_sub = np.linalg.inv(np.cov(npvec1, npvec2))
return math.sqrt(np.dot(inv_sub, sub).dot(sub.T))
# MahalanobisDistance

 

11. 编辑距离

def Edit_distance_str(str1, str2):
import Levenshtein
edit_distance_distance = Levenshtein.distance(str1, str2)
similarity = 1-(edit_distance_distance/max(len(str1), len(str2)))
return {'Distance': edit_distance_distance, 'Similarity': similarity}
# Levenshtein distance

http://bigdata-madesimple.com/implementing-the-five-most-popular-similarity-measures-in-python/

posted @ 2019-08-07 14:58  叶小雨  阅读(1466)  评论(0编辑  收藏  举报