zoukankan      html  css  js  c++  java
  • 机器学习各种相似性度量及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/

  • 相关阅读:
    使用git将本地代码上传到gitee【码云】
    .net设置实例字段在请求参数中不展示
    Springboot 操作Elasticsearch 方式一 【spring-data-elasticsearch】
    Java工具类 (3)------>WordUtils------>利用Poi根据模板生成新的word文档
    Java工具类 (2)------>TreeUtils------>树形结构生成类
    Java工具类 (1)------>IPUtils------>获取用户登录IP地址
    Idea安装MyBatisCodeHelper-Pro插件破解版以及去除mybatis的mapper.xml文件背景颜色
    Python 线程队列 LifoQueue – LIFO
    Python 线程队列 Queue – FIFO
    Python 线程障碍对象 Barrier
  • 原文地址:https://www.cnblogs.com/taoyuyeit/p/11315285.html
Copyright © 2011-2022 走看看