adjusted_rand_score#

sklearn.metrics.adjusted_rand_score(labels_true, labels_pred)[source]#

经过机会调整的 Rand index。

兰德指数通过考虑所有样本对,并计算在预测和真实聚类中被分配到相同或不同簇的对数,来计算两个聚类之间的相似度量。

然后使用以下方案将原始RI分数“调整为机会”以得到ARI分数

ARI = (RI - Expected_RI) / (max(RI) - Expected_RI)

因此,调整后的兰德指数(adjusted Rand index)确保对于随机标记,其值接近0.0,而与簇和样本的数量无关;当聚类完全相同时(允许置换),其值恰好为1.0。对于特别不一致的聚类,调整后的兰德指数下限为-0.5。

ARI是一个对称度量

adjusted_rand_score(a, b) == adjusted_rand_score(b, a)

用户指南中了解更多信息。

参数:
labels_true形状为 (n_samples,) 的类数组,dtype=int

用作参考的真实类别标签。

labels_pred形状为 (n_samples,) 的类数组,dtype=int

要评估的簇标签。

返回:
ARIfloat

介于-0.5和1.0之间的相似度分数。随机标记的ARI接近0.0。1.0表示完美匹配。

另请参阅

adjusted_mutual_info_score

调整互信息(Adjusted Mutual Information)。

References

[Hubert1985]

L. Hubert and P. Arabie, Comparing Partitions, Journal of Classification 1985 https://link.springer.com/article/10.1007%2FBF01908075

[Steinley2004]

D. Steinley, Properties of the Hubert-Arabie adjusted Rand index, Psychological Methods 2004

示例

即使是完美匹配的标记,分数也为1

>>> from sklearn.metrics.cluster import adjusted_rand_score
>>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 1])
1.0
>>> adjusted_rand_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0

将所有类别成员分配到同一簇的标记是完整的,但可能并不总是纯净的,因此会受到惩罚

>>> adjusted_rand_score([0, 0, 1, 2], [0, 0, 1, 1])
0.57

ARI是对称的,因此具有纯簇但成员来自同一类别但进行了不必要拆分的标记会受到惩罚

>>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 2])
0.57

如果类别成员完全分散到不同的簇中,则分配完全不完整,因此ARI非常低

>>> adjusted_rand_score([0, 0, 0, 0], [0, 1, 2, 3])
0.0

对于特别不一致的标记,ARI可能会取负值,这比随机标签的期望值更差

>>> adjusted_rand_score([0, 0, 1, 1], [0, 1, 0, 1])
-0.5

有关更详细的示例,请参阅聚类性能评估中的机会调整