python k-means群集使用matplotlib而不是质心进行绘图

时间:2018-12-04 02:04:45

标签: matplotlib k-means

我正在尝试绘制k均值分类。该代码可以很好地识别集群(而不是绘制集群),并且 可以绘制质心; 我正在尝试绘制集群可视化:但是我很难在此代码中实现

请在plt.scatter中绘制k表示簇(而不是质心)?

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score


import csv
from sklearn.metrics import accuracy_score
import csv
import matplotlib.pyplot as plt



documents = [line.strip() for line in open("internetsheet2.csv", 'r')]

vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(documents)
true_k = 3
model = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1)
model.fit(X)

print("Top terms per cluster:")
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
    print("Cluster %d:" % i),
    for ind in order_centroids[i, :10]:
        print(' %s' % terms[ind]),
    print



print("\n")


centroids = model.cluster_centers_
transformed_centroids = model.fit_transform(centroids)
plt.scatter(transformed_centroids[:, 0], transformed_centroids[:, 1], marker='x')
plt.show()

0 个答案:

没有答案