简体   繁体   English

SKLearn KMeans 收敛警告

[英]SKLearn KMeans Convergence Warning

I am using SKLearn's KMeans clustering on a 1D dataset.我在一维数据集上使用 SKLearn 的 KMeans 聚类。 The bug I am getting is that when I run the code, I am getting a ConvergenceWarning :我得到的错误是,当我运行代码时,我收到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)

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.我相信我的错误要么是因为我有一个一维数据结构,要么是因为我在 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

Some demo IO: Sample input:一些演示 IO:示例输入:

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

Sample output:示例输出:

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

Expected output:预期输出:

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 uses the warning module to raise warnings. Sklearn 使用warning模块来发出警告。 We can suppress the warnings as shown below.我们可以抑制警告,如下所示。

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

All warnings are suppressed within the with block so this functionality should be used with caution.所有警告都在with块中被抑制with因此应谨慎使用此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM