Python 寻找这个K-Means模型的准确度

Python 寻找这个K-Means模型的准确度,python,k-means,Python,K Means,该程序预测坐标所属的簇,将给定点分为两个簇0和1。 我如何获得变量预测模型的准确性 import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans #from sklearn.metrics import accuracy_score X = np.array([[1, 2],[5, 8],[1.5, 1.8],[8, 8],[6,7],[9, 11]]) print(

该程序预测坐标所属的簇,将给定点分为两个簇0和1。 我如何获得变量预测模型的准确性

import numpy as np  
import matplotlib.pyplot as plt  
from sklearn.cluster import KMeans  
#from sklearn.metrics import accuracy_score  
X = np.array([[1, 2],[5, 8],[1.5, 1.8],[8, 8],[6,7],[9, 11]])  
print(X)  
kmeans = KMeans(n_clusters=2)  
kmeans.fit(X)  
centroids = kmeans.cluster_centers_  
labels = kmeans.labels_  
print("Centroids :\n ",centroids)  
print("Labels    : ",labels)  
colors = ["g.","r.","c.","y."]  
for i in range(len(X)):  
print("coordinate:",X[i], "label:", labels[i])  
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize = 10)  
plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths 
= 5, zorder = 10)  
plt.show() 
prediction=kmeans.predict ( [ [ 5,6 ] ] )   
print(prediction)  

如果您知道坐标标签的正确值,可以使用scikit学习:


不过,对于集群问题来说,这似乎很棘手。思考如何确定预测是否正确,并计算预测的准确度。

对于变量预测,此模型的准确度是什么意思?我猜新点上的predict方法只会返回其质心最接近新点的簇的标签。你已经有一些已知的标签了吗?[1.2.]1[5.8.]0[1.5 1.8]1[8.8.]0[6.7.]0这些0和1是标签,这些0和1是kmeans聚类的结果吗?或者它们是程序运行前您知道的标签?在第一种情况下,计算精度是不可能的,因为您没有地面真理标签。在第二种情况下,可以将精度计算为正确指定的标签数除以点数。np.sumtrue\u标签==预测/lentrue\u标签
from sklearn.metrics import accuracy_score
print(accuracy_score(y_true, y_pred))