我有一组使用聚类算法(在本例中为k-均值)聚类的点。我也知道真实的标签,我想衡量我的聚类有多精确。我需要的是找到真正的准确性。当然,问题在于聚类给出的标签与原来的顺序不匹配。
有办法测量这种准确性吗?直观的想法是计算每个标签组合的混淆矩阵的分数,并且只保留最大值。有这样的功能吗?
我也用兰德分数和调整后的兰德评分来评估我的结果。这两种测量方法离实际准确度有多近?
谢谢!
发布于 2019-12-16 14:14:58
首先,The problem, of course, is that the labels given by the clustering do not match the ordering of the original one.是什么意思?
如果你知道基本的真实标签,那么你可以重新排列它们来匹配X矩阵的顺序,这样,Kmeans在预测之后就会与真正的标签一致。
在这种情况下,我建议如下。
概述:
完整示例:
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))发布于 2019-12-16 15:23:50
因为聚类是一个无监督的学习问题,所以您有其特定的度量标准:https://scikit-learn.org/stable/modules/classes.html#clustering-metrics
您可以参考scikit-learn用户指南中的讨论,以了解用于集群的不同指标之间的差异:https://scikit-learn.org/stable/modules/clustering.html#clustering-performance-evaluation
例如,调整后的兰德指数将比较一对点,并检查如果标签在地面上是相同的-真理,它将是相同的预测。与准确性不同的是,您不能使严格的标签相等。
发布于 2019-12-16 13:37:24
如下面提到的链接所示,您可以使用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
https://stackoverflow.com/questions/59346797
复制相似问题