试试^{}。根据均值漂移的skleanuser guide:The algorithm automatically sets the number of clusters, ...
修改后的演示代码:import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
# #############################################################################
# Generate sample data
X = [1,2,4,7,9,5,4,7,9,56,57,54,60,200,297,275,243]
X = np.reshape(X, (-1, 1))
# #############################################################################
# Compute clustering with MeanShift
# The following bandwidth can be automatically detected using
# bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=100)
ms = MeanShift(bandwidth=None, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)
print("number of estimated clusters : %d" % n_clusters_)
print(labels)
输出:
^{pr2}$
请注意,MeanShift是可伸缩的,可随样本数量而变化。建议上限为10000。在
顺便说一句,正如rahlf23所提到的,K-mean是一种无监督的学习算法。您必须指定集群的数量并不意味着它是受监控的。在
另请参见: