python - Kmeans 肘部方法不返回肘部

标签 python pandas scikit-learn cluster-analysis

所以按照文档中的示例 (here):

The KElbowVisualizer implements the “elbow” method to help data scientists select the optimal number of clusters by fitting the model with a range of values for K. If the line chart resembles an arm, then the “elbow” (the point of inflection on the curve) is a good indication that the underlying model fits best at that point.

图表中没有弯头怎么办?当我在我的数据集上运行相同的代码时,输​​出是: enter image description here

因此,每增加一个集群,失真分数就会不断增加。

但是,当我使用 kmeans.intertia 属性运行肘法的另一个示例时:

sse = {}
for k in range(1, 10):
    kmeans = KMeans(n_clusters=k, max_iter=1000).fit(testDF)
    testDF["clusters"] = kmeans.labels_
    #print(data["clusters"])
    sse[k] = kmeans.inertia_ # Inertia: Sum of distances of samples to their 
closest cluster center
plt.figure()
plt.plot(list(sse.keys()), list(sse.values()))
plt.xlabel("Number of cluster")
plt.ylabel("SSE")
plt.show()

输出是:

enter image description here

它确实有一个肘部。

这两种方法有什么区别? 为什么第一张图没有弯头?

根据文档,它们都应用相同的距离方法,即“样本到最近的聚类中心的距离平方和。”

最佳答案

我刚才遇到了同样的问题,更新到 Yellowbrick v1.1 解决了它。

pip install -U yellowbrick

或在 Jupyter 单元中:

!pip install -U yellowbrick

关于python - Kmeans 肘部方法不返回肘部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54423439/

相关文章:

python - XLSX Writer 格式化前 n 列的行边框

python - 比较两列,如果满足条件,则将 '1' 添加到新列

python - NetworkX - 从数据帧设置节点属性

python - MAC OS下如何使用python检测应用程序是否运行以及是否显示消息框

python - 检查当前行中的所有列值是否小于 Pandas 数据框中的前一行

python - 从 Kafka 轮询几条消息

python - 寻求比较和过滤 Pandas 日期范围重叠的有效方法

python - 如何使用 scikit-learn 获得优势比和其他相关特征

Python:调用继承的父类方法失败

python - XGBOOST:sample_Weights 与 scale_pos_weight