可视化Kmeans返回的集群

时间:2019-04-24 20:21:48

标签: python python-3.x matplotlib scikit-learn plotly

我使用KMeans进行集群,如下所示,但是我不知道如何可视化集群(如下图所示)以查看客户满意度。 example

代码:

tail

1 个答案:

答案 0 :(得分:0)

让我们想象一下,您有一种方法可以知道k均值的哪个分区代表哪种情感,您可以按照以下方式绘制饼图:

print(model.labels_)  # For illustration, you can see which sentence is in which cluster
# Here we get the proportions
nb_samples = [sum(model.labels_ == j) for j in range(true_k)]

# On the next line the order is RANDOM. I do NOT know which cluster represents what.
# The first label should represent samples in cluster 0, and so on
labels = 'positive', 'neutral', 'negative'
colors = ['gold', 'red', 'lightblue']  # Same size as labels

# Pie chart
plt.pie(nb_samples, labels=labels, colors=colors, autopct='%1.1f%%')
plt.axis('equal')
plt.show()

同样,多次运行会给您不同的结果,即哪个簇代表哪个类别。

可以通过设置numpy随机种子来避免这种情况。

import numpy as np
np.random.seed(42)  # Or any other integer