當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sklearn kmeans_plusplus用法及代碼示例

本文簡要介紹python語言中 sklearn.cluster.kmeans_plusplus 的用法。

用法:

sklearn.cluster.kmeans_plusplus(X, n_clusters, *, x_squared_norms=None, random_state=None, n_local_trials=None)

根據k-means++初始化n_clusters種子

參數

X{類數組,稀疏矩陣},形狀為 (n_samples, n_features)

從中挑選種子的數據。

n_clustersint

要初始化的質心數

x_squared_norms形狀類似數組 (n_samples,),默認=None

每個數據點的平方歐幾裏得範數。

random_stateint 或 RandomState 實例,默認=無

確定質心初始化的隨機數生成。傳遞 int 以在多個函數調用之間實現可重現的輸出。請參閱術語表。

n_local_trials整數,默認=無

每個中心的播種試驗次數(第一次除外),其中最減少慣性的一個被貪心地選擇。設置為 None 使試驗次數與種子數成對數關係 (2+log(k))。

返回

centersndarray 形狀(n_clusters,n_features)

k-means 的初始中心。

indicesndarray 形狀 (n_clusters,)

數據數組 X 中所選中心的索引位置。對於給定的索引和中心,X[index] = center。

注意

以智能方式為k-mean 聚類選擇初始聚類中心以加速收斂。參見:Arthur, D. 和 Vassilvitskii, S. “k-means++:仔細播種的優勢”。 ACM-SIAM 離散算法研討會。 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,  4],
       [ 1,  0]])
>>> indices
array([4, 2])

相關用法


注:本文由純淨天空篩選整理自scikit-learn.org大神的英文原創作品 sklearn.cluster.kmeans_plusplus。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。