SKLearn KMeans 收敛警告

5

我正在使用SKLearn的KMeans聚类算法处理一个一维数据集。遇到的问题是,当我运行代码时,会出现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中的一维数组时出了问题。以下是有问题的代码:

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: 样例输入:
[(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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( 2.,) ( 2.,) ( 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.,)]

示例输出:

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

您可能会注意到输入中有很多重复的值。这是正常现象,我想知道如何更好地聚类这些数据,以便避免出现具有重复质心的重复聚类。

1个回答

5

理想情况下,指定的簇数不应超过唯一数据点的数量。如果您能相应地调整质心计数,那么将不会出现警告。

Sklearn使用warning模块来发出警告。我们可以按照下面所示的方法抑制这些警告。

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

with块内部,所有警告都被抑制,因此应该谨慎使用此功能。


“超过唯一数据点的数量”是一个不清晰的陈述。您是指变量还是值?此外,使用这个引用短语,您是在暗示降维还是特征选择?再次强调,重点缺失。请尝试进一步改进此答案。 - mnm
MWE的数据是单维的,因此不适用降维或特征选择。同样地,既然我和OP都没有提到这些想法,强行加入回复中可能会让读者感到困惑。这里的“唯一数据点”是指唯一示例的数量(即删除任何重复示例/点后的数量)。正如OP所提到的并且从MWE中可以看出,他有许多重复的点。 OP的原始数据集结构化为一个多重集合,但与聚类数(即n_clusters)进行比较时,sklearn将该多重集合转换为一个集合。 - ZaydH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接