• 常见的相似或相异程度计算方法


    如何衡量数据点之间的相似或相异程度是聚类算法的基础问题,会直接影响聚类分析的效果,最直观的方法是使用距离函数或者相似性函数。

    常见的相似或相异程度计算方法。

    1.计算公式

    1.Minkowski distance

    很多距离计算方法都可以归结为基于向量p范数的距离,即Minkowski distance。

    (d_{ij} = ({sum_{h=1}^{s}|x_{ih}-x_{jh}|^{p}})^{1/p})

    2.Euclidean distance

    参数p = 2,Minkowski distance退化为Euclidean distance,使用Euclidean distance的聚类算法大多只能发现低维空间中呈超球分布的数据,并且对数据集中的噪声比较敏感。

    (d_{ij} = ({sum_{h=1}^{s}|x_{ih}-x_{jh}|^{2}})^{1/2})

    3.City-block distance

    参数p = 1,Minkowski distance演变为City-block distance,City-block distance可以有效提高模糊聚类算法对噪声或者孤立点的鲁棒性。

    (d_{ij} = {sum_{h=1}^{s}|x_{ih}-x_{jh}|})

    4.Sup distance

    参数p = 无穷,Minkowski distance演变为Sup distance。

    (d_{ij} = max_{h}|x_{ih}-x_{jh}|)

    5.Cosine similarity

    (s_{ij} = frac{x_{i}^{T}x_{j}}{||x_{i}||||x_{j}||})

    6.Mahalanobis distance

    Mahalanobis distance为原特征空间中的数据在线性投影空间欧式距离,使用Mahalanobis distance能够使得聚类算法成功发现数据集里成超椭球型分布的类簇,但是Mahalanobis distance会带来较大的计算量。

    (d_{ij} = (x_{i} - x_{j})^{T}S^{-1}(x_{i} - x_{j}))

    7.Alternative distance

    Alternative distance对数据集里的噪声不敏感。

    (d_{ij} = 1 - exp(-eta||x_{i} - x_{j}||^{2}))

    8.Feature weighted distance

    (d_{ij} = (sum_{h=1}^{s}w_{h}^{a}|x_{ih} - x_{jh}|)^{1/2})

    2.代码

    代码,

    import numpy as np
    a = np.array([1,2,3,4])
    b = np.array([4,3,2,1])
    print a
    print b
    
    #Euclidean distance
    distEu = np.sqrt(np.sum((a-b)**2))
    print "Euclidean distance = ",distEu
    
    #City-block distance
    distCb = np.sum(np.abs(a-b))
    print "City-block distance = ",distCb
    
    #Sup distance
    distSup = max(np.abs(a-b))
    print "Sup distance = ",distSup
    
    #Cosine similarity
    cosineSimi = np.dot(a,b) / (np.sqrt(np.sum(a**2)) * np.sqrt(np.sum(b**2)))
    print "Cosine similarity = ",cosineSimi
    
    #Alternative distance
    beta = 0.5
    distAlter = 1 - np.exp(-beta * np.sqrt(np.sum((a - b)**2)))
    print "Alternative distance = ",distAlter
    
    #Feature weighted distance
    weigh = np.array([0.5,0.3,0.1,0.1])
    distFea = np.sqrt(np.dot(weigh,np.abs(a-b)))
    print "Feature weighted distance = ",distFea
    

    输出,

    [1 2 3 4]
    [4 3 2 1]
    Euclidean distance =  4.472135955
    City-block distance =  8
    Sup distance =  3
    Cosine similarity =  0.666666666667
    Alternative distance =  0.89312207434
    Feature weighted distance =  1.48323969742
    

    3.Reference

    王骏,王士同,邓赵红.聚类分析研究中的若干问题[J].控制与决策,2012年第3期,Vol27 No.3;

  • 相关阅读:
    20165334 第十三周选做
    20165334 实验五 网络编程与安全
    ### 20165334 —— 第十二周MySort(选作)
    2018-2019-1 20165227 20165228 20165237 实验一 开发环境的熟悉
    2018-2019-1 20165228 《信息安全系统设计基础》第四周学习总结
    2018-2019-1 20165228《信息安全系统设计基础》第三周课上测试
    2018-2019-1 20165228 《信息安全系统设计基础》第三周学习总结
    2018-2019-1 20165228 《信息安全系统设计基础》缓冲区溢出漏洞实验报告
    2018-2019-1 20165228 《信息安全系统设计基础》第二周学习总结
    2018-2019-1 20165228 苏祚堃《信息安全系统设计基础》第一周学习总结
  • 原文地址:https://www.cnblogs.com/zhbzz2007/p/5869913.html
Copyright © 2020-2023  润新知