如何使均值漂移聚类支持超过五个簇?

1 投票
1 回答
2620 浏览
提问于 2025-04-17 13:50

我在使用均值漂移聚类时遇到了一些问题。它在聚类数量少的时候(比如2、3、4个聚类)运行得很快,结果也很准确,但当聚类数量增加时,它就出问题了。

比如说,3个聚类检测得很好:

cluster success

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

cluster centers fail clusters fail

这里是完整的代码列表:

#!/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()

我哪里做错了呢?

1 个回答

1

你可能需要选择一个更小的带宽。我对带宽是怎么通过启发式方法选择的不是很了解。所以这里的问题其实出在启发式方法上,而不是实际的算法。

撰写回答