sklearn聚类算法评估方法 之各种系数

python中的分群质量

主要参考来自官方文档:Clustering

部分内容来源于:机器学习评价指标大汇总
个人比较偏好的三个指标有:Calinski-Harabaz Index(未知真实index的模型评估)、Homogeneity, completeness and V-measure(聚类数量情况)、轮廓系数

1.1 Adjusted Rand index 调整兰德系数

这里写图片描述

>>> from sklearn import metrics
>>> labels_true = [0, 0, 0, 1, 1, 1]
>>> labels_pred = [0, 0, 1, 1, 2, 2]

>>> metrics.adjusted_rand_score(labels_true, labels_pred)  
0.24

.

1.2 Mutual Information based scores 互信息

这里写图片描述
Two different normalized versions of this measure are available, Normalized Mutual Information(NMI) and Adjusted Mutual Information(AMI). NMI is often used in the literature while AMI was proposed more recently and is normalized against chance:

>>> from sklearn import metrics
>>> labels_true = [0, 0, 0, 1, 1, 1]
>>> labels_pred = [0, 0, 1, 1, 2, 2]

>>> metrics.adjusted_mutual_info_score(labels_true, labels_pred)  
0.22504

.

1.3 Homogeneity, completeness and V-measure

同质性homogeneity:每个群集只包含单个类的成员。
完整性completeness:给定类的所有成员都分配给同一个群集。

>>> from sklearn import metrics
>>> labels_true = [0, 0, 0, 1, 1, 1]
>>> labels_pred = [0, 0, 1, 1, 2, 2]

>>> metrics.homogeneity_score(labels_true, labels_pred)  
0.66...

>>> metrics.completeness_score(labels_true, labels_pred) 
0.42...

两者的调和平均V-measure:

>>> metrics.v_measure_score(labels_true, labels_pred)    
0.51...

.

1.4 Fowlkes-Mallows scores

The Fowlkes-Mallows score FMI is defined as the geometric mean of the pairwise precision and recall:
这里写图片描述

>>> from sklearn import metrics
>>> labels_true = [0, 0, 0, 1, 1, 1]
>>> labels_pred = [0, 0, 1, 1, 2, 2]
>>>
>>> metrics.fowlkes_mallows_score(labels_true, labels_pred)  
0.47140...

.

1.5 Silhouette Coefficient 轮廓系数

这里写图片描述

>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> kmeans_model = KMeans(n_clusters=3, random_state=1).fit(X)
>>> labels = kmeans_model.labels_
>>> metrics.silhouette_score(X, labels, metric='euclidean')
...                                                      
0.55...

.

1.6 Calinski-Harabaz Index

这个计算简单直接,得到的Calinski-Harabasz分数值ss越大则聚类效果越好。Calinski-Harabasz分数值ss的数学计算公式是(理论介绍来源于:用scikit-learn学习K-Means聚类):

这里写图片描述
 也就是说,类别内部数据的协方差越小越好,类别之间的协方差越大越好,这样的Calinski-Harabasz分数会高。
 在scikit-learn中, Calinski-Harabasz Index对应的方法是metrics.calinski_harabaz_score.
在真实的分群label不知道的情况下,可以作为评估模型的一个指标。
同时,数值越小可以理解为:组间协方差很小,组与组之间界限不明显。
与轮廓系数的对比,笔者觉得最大的优势:快!相差几百倍!毫秒级

>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> kmeans_model = KMeans(n_clusters=3, random_state=1).fit(X)
>>> labels = kmeans_model.labels_
>>> metrics.calinski_harabaz_score(X, labels)  
560.39...
展开阅读全文

没有更多推荐了,返回首页