简体   繁体   中英

SKLearn KMeans Convergence Warning

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)

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

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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