Python 如何使均值漂移聚类适用于五个以上的聚类?

Python 如何使均值漂移聚类适用于五个以上的聚类?,python,scikit-learn,Python,Scikit Learn,我在均值漂移聚类方面遇到了问题。当簇数较小(2、3、4)时,它工作得非常快并输出正确的结果,但当簇数增加时,它失败 例如,可以检测到3个簇: 但当数量增加时,它就失败了: 下面是完整的代码列表: #!/usr/bin/env python import sys import logging import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plot from s

我在均值漂移聚类方面遇到了问题。当簇数较小(2、3、4)时,它工作得非常快并输出正确的结果,但当簇数增加时,它失败

例如,可以检测到3个簇:

但当数量增加时,它就失败了:

下面是完整的代码列表:

#!/usr/bin/env python

import sys
import logging

import numpy as np

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot

from sklearn.cluster import estimate_bandwidth, MeanShift, get_bin_seeds
from sklearn.datasets.samples_generator import make_blobs


def test_mean_shift():
    logging.debug('Generating mixture')
    count = 5000
    blocks = 7
    std_error = 0.5
    mixture, clusters = make_blobs(n_samples=count, centers=blocks, cluster_std=std_error)

    logging.debug('Measuring bendwith')
    bandwidth = estimate_bandwidth(mixture)
    logging.debug('Bandwidth: %r' % bandwidth)

    mean_shift = MeanShift(bandwidth=bandwidth)

    logging.debug('Clustering')
    mean_shift.fit(mixture)

    shifted = mean_shift.cluster_centers_
    guess = mean_shift.labels_

    logging.debug('Centers: %r' % shifted)

    def draw_mixture(mixture, clusters, output='mixture.png'):
        plot.clf()
        plot.scatter(mixture[:, 0], mixture[:, 1],
                     c=clusters,
                     cmap=plot.cm.coolwarm)
        plot.savefig(output)

    def draw_mixture_shifted(mixture, shifted, output='mixture_shifted.png'):
        plot.clf()
        plot.scatter(mixture[:, 0], mixture[:, 1], c='r')
        plot.scatter(shifted[:, 0], shifted[:, 1], c='b')
        plot.savefig(output)

    logging.debug('Drawing')
    draw_mixture_shifted(mixture, shifted)
    draw_mixture(mixture, guess)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    test_mean_shift()

我做错了什么?

您可能必须选择较小的带宽。我不太熟悉启发式选择带宽的方式。所以这里的“问题”在于启发式,而不是实际的算法。

您可能必须选择较小的带宽。我不太熟悉启发式选择带宽的方式。所以这里的“问题”在于启发式,而不是实际的算法。

看来yandex-team.ru上托管的图片的引用是不可操作的。在俄罗斯,它们甚至不通过直接链接加载。哦,真的很抱歉,我太笨了。现在能用了吗?看起来yandex-team.ru上的图片引用不起作用。它们甚至不能在俄罗斯通过直接链接加载。哦,真的很抱歉,我真蠢。现在能用了吗?