SKLearn KMeans收敛警告 [英] SKLearn KMeans Convergence Warning

查看:213
本文介绍了SKLearn KMeans收敛警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在1D数据集上使用SKLearn的KMeans聚类.我得到的错误是,当我运行代码时,我得到的是ConvergenceWarning:

I am using SKLearn's KMeans clustering on a 1D dataset. The bug I am getting is that when I run the code, I am getting a ConvergenceWarning:

ConvergenceWarning: Number of distinct clusters (<some integer n>) found smaller than n_clusters (<some integer bigger than n>). Possibly due to duplicate points in X.
  return_n_iter=True)

除了源代码外,我找不到任何其他东西,这没有表明到底出了什么问题.我相信我的错误是因为我具有一维数据结构,或者是因为我在SKLearn中使用一维数组的方式出了问题.这是令人反感的代码:

I cannot find anything on this aside from the source code, which does not indicate what exactly is going wrong. I believe my bug is either because I have a 1D data structure, or because something is going wrong in how I use the 1D array with SKLearn. Here is the offending code:

def cluster_data(data_arr):
    """clusters the uas for a specific site"""
    d = 1.0
    k = 1
    inertia_prev = 1.0
    while k <= MAX and d > DELTA: 
        #max is the size of the input array, delta is .05
        kmean = KMeans(n_clusters=k)
        prediction = kmean.fit_predict(data_arr.reshape(-1, 1))
        #bug could be in the reshape!
        inertia_curr = kmean.inertia_
        d = abs(1 - (inertia_curr / inertia_prev))
        inertia_prev = inertia_curr
        k += 1

一些演示IO: 样本输入:

Some demo IO: Sample input:

[(11.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 7.,) ( 0.,) ( 4.,) ( 7.,)
( 7.,) (13.,) ( 2.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,) ( 0.,)
 ( 7.,) ( 2.,) ( 0.,) ( 0.,) (11.,) ( 7.,) ( 7.,) ( 0.,) ( 2.,) ( 1.,)
 ( 0.,) ( 0.,) ( 0.,) ( 7.,) ( 5.,) ( 0.,) ( 0.,) ( 4.,) ( 0.,) ( 0.,)
 ( 0.,) ( 0.,) ( 8.,) ( 0.,)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) ( 7.,) (16.,) (15.,)
 (11.,) (15.,) (17.,) (11.,) (11.,) ( 7.,) (15.,) (15.,) (11.,) (11.,)
 (15.,) (15.,) (15.,) (16.,) (11.,) ( 7.,) (16.,) (11.,) (11.,) (15.,)
 (11.,) (15.,) ( 5.,) (16.,) (11.,) (11.,) ( 7.,) (15.,) (15.,) (15.,)]

示例输出:

ConvergenceWarning: Number of distinct clusters (14) found smaller than n_clusters (15). Possibly due to duplicate points in X. return_n_iter=True)

预期输出:

no warning

您可能会注意到输入中有很多重复的值.这是预料之中的,我想知道如何更好地对这些数据进行聚类,因此不会出现重复质心重复的聚类.

You may notice that the input has a lot of repeated values. This is expected, and I would like to know how I can better cluster this data, so I don't get repeated clusters with duplicate centroids.

推荐答案

理想情况下,指定的群集数量不应超过唯一数据点的数量.如果您可以相应地调整质心数,则将不会发出警告.

Ideally, the specified number of clusters should not exceed the number of unique data points. If you can adjust your centroid count accordingly, that will cause the warning not to be raised.

Sklearn使用warning模块引发警告.我们可以禁止显示警告,如下所示.

Sklearn uses the warning module to raise warnings. We can suppress the warnings as shown below.

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    cluster_data(data_arr)

with块中禁止显示所有警告,因此应谨慎使用此功能.

All warnings are suppressed within the with block so this functionality should be used with caution.

这篇关于SKLearn KMeans收敛警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆