【发布时间】:2020-09-09 22:40:12
【问题描述】:
我正在研究文本聚类。我需要使用不同的颜色绘制数据。
我使用kmeans 方法进行聚类,使用tf-idf 进行相似性。
kmeans_labels =KMeans(n_clusters=3).fit(vectorized_text).labels_
pipeline = Pipeline([('tfidf', TfidfVectorizer())])
X = pipeline.fit_transform(X_train['Sentences']).todense()
pca = PCA(n_components=2).fit(X)
data2D = pca.transform(X)
plt.scatter(data2D[:,0], data2D[:,1])
kmeans.fit(X)
centers2D = pca.transform(kmeans.cluster_centers_)
labels=np.array([kmeans.labels_])
目前,我的输出看起来像: 有一些元素,因为它是一个测试。 我需要添加标签(它们是字符串)并按集群区分点:每个集群都应该有自己的颜色,以使读者易于分析图表。
您能告诉我如何更改代码以同时包含标签和颜色吗?我认为任何例子都会很棒。
我的数据集的一个样本是(上面的输出是从不同的样本生成的):
句子
Where do we do list them? ...
Make me a list of the things we would need and I'll take you into town. ...
Do you have a list yet? ...
The first was a list for Howie. ...
You're not on my list tonight. ...
I'm gonna print this list on my computer, given you're always bellyaching about my writing.
【问题讨论】:
-
这里我看到了使用
plotly的完美案例。您介意提供mcve 吗?至少你原来的 df 有一列集群。 -
这个有帮助吗adding colors and labels
-
@rpanai,请查看更新后的问题。
-
@CarlosAzevedo,我怎样才能相应地编辑我的代码?
-
@still_learning 我提供了它作为答案
标签: python matplotlib cluster-analysis k-means tf-idf