kmeans_plusplus#
- sklearn.cluster.kmeans_plusplus(X, n_clusters, *, sample_weight=None, x_squared_norms=None, random_state=None, n_local_trials=None)[source]#
根据 k-means++ 初始化 n_clusters 种子。
0.24 版本新增。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
用于选择种子的数据。
- n_clustersint
要初始化的质心数量。
- sample_weightshape 为 (n_samples,) 的 array-like, default=None
数据集中每个观测值的权重。如果为
None,则所有观测值都被分配相等的权重。如果init是一个可调用对象或用户提供的数组,则忽略sample_weight。在版本 1.3 中新增。
- x_squared_norms形状为 (n_samples,) 的类数组,默认值=None
每个数据点的欧几里德范数的平方。
- random_stateint 或 RandomState 实例,默认值=None
确定质心初始化的随机数生成。传入一个 int 以在多次函数调用中获得可重现的输出。请参阅 Glossary。
- n_local_trialsint,默认值=None
每个中心(除了第一个)的播种试验次数,其中减少惯性最多的一个被贪婪地选择。设置为 None 使试验次数对种子数量呈对数依赖(2+log(k)),这是推荐的设置。设置为 1 会禁用贪婪聚类选择,并恢复原始的 k-means++ 算法,该算法在经验上显示效果不如其贪婪变体。
- 返回:
- centers形状为 (n_clusters, n_features) 的 ndarray
k-means 的初始中心。
- indices形状为 (n_clusters,) 的 ndarray
所选中心在数据数组 X 中的索引位置。对于给定的索引和中心,X[index] = center。
注意事项
以一种智能的方式选择 k-均值聚类的初始聚类中心,以加快收敛速度。参见:Arthur, D. and Vassilvitskii, S. “k-means++: the advantages of careful seeding”. ACM-SIAM symposium on Discrete algorithms. 2007
示例
>>> from sklearn.cluster import kmeans_plusplus >>> import numpy as np >>> X = np.array([[1, 2], [1, 4], [1, 0], ... [10, 2], [10, 4], [10, 0]]) >>> centers, indices = kmeans_plusplus(X, n_clusters=2, random_state=0) >>> centers array([[10, 2], [ 1, 0]]) >>> indices array([3, 2])