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

X 中每个观测值的权重。如果为 None,则所有观测值权重相等。如果 init 是一个可调用对象或用户提供的数组,则忽略 sample_weight

在版本 1.3 中新增。

x_squared_norms形状为 (n_samples,) 的类数组对象,默认=None

每个数据点的欧几里得范数的平方。

random_stateint 或 RandomState 实例,默认=None

确定用于质心初始化的随机数生成。传入一个 int 以在多次函数调用中获得可复现的输出。请参阅 术语表

n_local_trialsint,默认=None

每个中心点(第一个除外)的种子试验次数,其中减少惯性(inertia)最多的那次会被贪婪地选中。设置为 None 时,试验次数将取决于种子数量的对数 (2+log(k)),这是推荐设置。设置为 1 将禁用贪婪聚类选择,并恢复为原始的 k-means++ 算法,该算法经经验证明效果不如其贪婪变体。

返回:
centers形状为 (n_clusters, n_features) 的 ndarray

k-means 的初始中心点。

indices形状为 (n_clusters,) 的 ndarray

所选中心点在数据数组 X 中的索引位置。对于给定的索引和中心点,X[index] = center。

注意事项

以智能方式选择 k-means 聚类的初始簇中心,以加速收敛。参见: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])