【问题标题】:What is the accuracy of a clustering algorithm?聚类算法的准确性是多少?
【发布时间】:2020-04-08 08:04:33
【问题描述】:

我有一组使用聚类算法(在本例中为 k-means)聚类的点。我也知道真实标签,我想衡量我的聚类有多准确。我需要的是找到实际的准确性。当然,问题在于聚类给出的标签与原始标签的顺序不匹配。

有没有办法衡量这种准确性?直观的想法是计算每个标签组合的混淆矩阵的分数,并且只保留最大值。有没有这样做的功能?

我还使用 rand 分数和调整后的 rand 分数评估了我的结果。这两个指标与实际准确度有多接近?

谢谢!

【问题讨论】:

  • “混淆矩阵的分数”是什么意思?

标签: scikit-learn cluster-computing


【解决方案1】:

您可以使用下面提到的链接中记录的 sklearn.metrics.accuracy

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html

一个例子可以在下面提到的链接中看到

sklearn: calculating accuracy score of k-means on the test data set

【讨论】:

    【解决方案2】:

    首先,The problem, of course, is that the labels given by the clustering do not match the ordering of the original one.是什么意思?

    如果您知道ground truth标签,那么您可以重新排列它们以匹配X矩阵的顺序,这样,Kmeans标签将与预测后的真实标签一致。


    在这种情况下,我建议如下。

    • 如果您有 ground truth 标签,并且想要查看模型的准确度,那么您需要 Rand 指数或预测标签与真实标签之间的互信息等指标。您可以在交叉验证方案中执行此操作,并查看模型的行为方式,即它是否可以正确预测交叉验证方案下的类/标签。可以使用兰德指数等指标来计算预测优度的评估。

    总结:

    • 定义 Kmeans 模型并使用交叉验证,并在每次迭代中估计 分配真实标签 之间的 Rand 指数(或互信息)。对所有迭代重复此操作,最后取 Rand 指数分数的平均值。如果这个分数很高,那么模型就很好。

    完整示例:

    from sklearn.cluster import KMeans
    from sklearn.metrics.cluster import adjusted_rand_score
    from sklearn.datasets import load_iris
    from sklearn.model_selection import LeaveOneOut
    import numpy as np
    
    # some data
    data = load_iris()
    X = data.data
    y = data.target # ground truth labels
    loo = LeaveOneOut()
    
    rand_index_scores = []
    for train_index, test_index in loo.split(X): # LOOCV here
       X_train, X_test = X[train_index], X[test_index]
       y_train, y_test = y[train_index], y[test_index]
    
       # the model
       kmeans = KMeans(n_clusters=3, random_state=0)
       kmeans.fit(X_train) # fit using training data
       predicted_labels = kmeans.predict(X_test) # predict using test data
       rand_index_scores.append(adjusted_rand_score(y_test, predicted_labels)) # calculate goodness of predicted labels
    
    print(np.mean(rand_index_scores))
    

    【讨论】:

    • 谢谢!我还有几个问题:为什么我需要原始数据(在你的情况下是 X)?我已经在我的数据集上尝试过,我得到的准确性比随机猜测要差得多,这绝对是错误的。总而言之,当我有 10 个集群时,为什么会得到三个不同的答案?再次感谢您!
    【解决方案3】:

    由于聚类是一个无监督学习问题,因此您有特定的指标:https://scikit-learn.org/stable/modules/classes.html#clustering-metrics

    您可以参考 scikit-learn 用户指南中的讨论,了解不同集群指标之间的差异:https://scikit-learn.org/stable/modules/clustering.html#clustering-performance-evaluation

    例如,调整后的 Rand 指数将比较一对点,并检查标签是否在基本事实中相同,在预测中是否相同。与准确性不同,您不能进行严格的标签相等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 2021-12-14
      • 2018-12-21
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多