此程序预测坐标所属的簇,它将给定点分为两个簇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)
答案 0 :(得分:1)
如果知道坐标标签的正确值,则可以使用scikit-learn's
accuracy_score
:
from sklearn.metrics import accuracy_score
print(accuracy_score(y_true, y_pred))
这对于群集问题确实有些棘手。考虑一下如何确定预测是否正确,并计算出准确度。