答案 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