聚类模型评价(python)

原文链接:聚类模型评价(python实现)

微信公众号:机器学习养成记 搜索添加微信公众号:chenchenwings


在使用聚类方法的过程中,常常涉及到如何选择合适的聚类数目、如何判断聚类效果等问题,本篇文章我们就来介绍几个聚类模型的评价指标,并展示相关指标在python中的实现方法。

概述

评价指标分为外部指标和内部指标两种,外部指标指评价过程中需要借助数据真实情况进行对比分析,内部指标指不需要其他数据就可进行评估的指标。下表中列出了几个常用评价指标的相关情况:

python实现

轮廓系数(Silhouette Coefficient)

轮廓系数可以用来选择合适的聚类数目。根据折线图可直观的找到系数变化幅度最大的点,认为发生畸变幅度最大的点就是最好的聚类数目。

from sklearn.metrics import silhouette_score

data2 = data1.sample(n=2000,random_state=123,axis=0)

silhouettescore=[]

for i in range(2,8):

    kmeans=KMeans(n_clusters=i,random_state=123).fit(data2.iloc[:,1:4])

    score=silhouette_score(data2.iloc[:,1:4],kmeans.labels_)

    silhouettescore.append(score)

plt.figure(figsize=(10,6))

plt.plot(range(2,8),silhouettescore,linewidth=1.5,linestyle='-')

plt.show()

数目在2到3时畸变程度越大,因此选择2类较好。

Calinski-Harabaz 指数

Calinski-Harabaz指数也可以用来选择最佳聚类数目,且运算速度远高于轮廓系数,因此个人更喜欢这个方法。当内部数据的协方差越小,类别之间的协方差越大,Calinski-Harabasz分数越高。

from sklearn.metrics import calinski_harabaz_score

for i in range(2,7):

    kmeans=KMeans(n_clusters=i,random_state=123).fit(data2.iloc[:,1:4])

    score=calinski_harabaz_score(data2.iloc[:,1:4],kmeans.labels_)

    print('聚类%d簇的calinski_harabaz分数为:%f'%(i,score))

#聚类2簇的calinski_harabaz分数为:3535.009345

#聚类3簇的calinski_harabaz分数为:3153.860287

#聚类4簇的calinski_harabaz分数为:3356.551740

#聚类5簇的calinski_harabaz分数为:3145.500663

#聚类6簇的calinski_harabaz分数为:3186.529313

可见,分为两类的值最高,结论与上面的轮廓系数判断方法一致。

调整兰德系数(Adjusted Rand index,ARI)

从兰德系数开始,为外部指标。兰德指数用来衡量两个分布的吻合程度,取值范围[-1,1],数值越接近于1越好,并且在聚类结果随机产生时,指标接近于0。为方便演示,省去聚类过程,直接用样例数据展示实现方法。

from sklearn.metrics import adjusted_rand_score

labels_true = [0, 0, 1, 1, 0, 1]

labels_pred = [0, 0, 1, 1, 1, 2]

ari=adjusted_rand_score(labels_true, labels_pred) 

print('兰德系数为:%f'%(ari))

#兰德系数为:0.117647

互信息(Adjusted Mutual Information,AMI)

互信息也是用来衡量两个分布的吻合程度,取值范围[-1,1],值越大聚类效果与真实情况越吻合。

from sklearn.metrics import adjusted_mutual_info_score

labels_true = [0, 0, 1, 1, 0, 1]

labels_pred = [0, 0, 1, 1, 1, 2]

ami=adjusted_mutual_info_score(labels_true, labels_pred)

print('互信息为:%f'%(ami))

#互信息为:0.225042

V-measure

说V-measure之前要先介绍两个指标:

同质性(homogeneity):每个群集只包含单个类的成员。

完整性(completeness):给定类的所有成员都分配给同一个群集。

V-measure是两者的调和平均。V-measure取值范围为 [0,1],越大越好,但当样本量较小或聚类数据较多的情况,推荐使用AMI和ARI。

from sklearn import metrics

labels_true = [0, 0, 0, 1, 1, 1]

labels_pred = [0, 0, 1, 1, 2, 2]

h_score=metrics.homogeneity_score(labels_true, labels_pred)

c_score=metrics.completeness_score(labels_true, labels_pred)

V_measure=metrics.v_measure_score(labels_true, labels_pred)   

print('h_score为:%f \nc_score为:%f \nV_measure为:%f'%(h_score,c_score,V_measure))

#h_score为:0.666667

#c_score为:0.420620

#V_measure为:0.515804

Fowlkes-Mallows Index(FMI)

FMI是对聚类结果和真实值计算得到的召回率和精确率,进行几何平均的结果,取值范围为 [0,1],越接近1越好。

from sklearn.metrics import fowlkes_mallows_score

labels_true = [0, 0, 0, 1, 1, 1]

labels_pred = [0, 0, 1, 1, 2, 2]

fmi=fowlkes_mallows_score(labels_true, labels_pred) 

print('FMI为:%f'%(fmi))

#FMI为:0.471405

一般情况下,主要是对无y值的数据进行聚类操作。如果在评价中用到外部指标,就需通过人工标注等方法获取y值,成本较高,因此内部指标的实际实用性更强。


推荐文章

·Bagging算法(R语言)

·静态爬虫与地址经纬度转换(python)

·特征工程(一):前向逐步回归(R语言)

·聚类(三):KNN算法(R语言)

·小案例(六):预测小偷行为(python)

·ggplot2:正负区分条形图及美化

扫描二维码,关注我们。

如需转载,请在开篇显著位置注明作者和出处,并在文末放置机器学习养成记二维码和添加原文链接。

快来关注我们吧!

你可能感兴趣的:(聚类模型评价(python))