Kmeans肘法不返回肘

时间:2019-01-29 14:38:56

标签: python pandas scikit-learn cluster-analysis

因此,按照文档(here)中的示例进行操作:

  

KElbowVisualizer实现“肘”方法来帮助数据科学家   通过使模型具有以下范围来选择最佳聚类数    K的值。如果折线图类似于手臂,则“弯头”(   曲线上的拐点)很好地表明了   该模型最适合那时。

如果图表中没有肘部怎么办?当我在数据集上运行相同的代码时,输​​出为: 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

有肘部。

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

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

1 个答案:

答案 0 :(得分:1)

我现在遇到了同样的问题,并更新到Yellowbrick v1.1修复了它。

pip install -U yellowbrick

或在Jupyter单元格中:

!pip install -U yellowbrick