kmeans_plusplus#
- sklearn.cluster.kmeans_plusplus(X, n_clusters, *, sample_weight=None, x_squared_norms=None, random_state=None, n_local_trials=None)[原始碼]#
根據 k-means++ 初始化 n_clusters 個種子。
於 0.24 版本新增。
- 參數:
- X{類陣列, 稀疏矩陣},形狀為 (n_samples, n_features)
用於選擇種子的資料。
- n_clusters整數
要初始化的質心數量。
- sample_weight類陣列,形狀為 (n_samples,),預設為 None
X
中每個觀測值的權重。如果為None
,則所有觀測值都被賦予相等的權重。如果init
是可呼叫的或使用者提供的陣列,則會忽略sample_weight
。於 1.3 版本新增。
- x_squared_norms類陣列,形狀為 (n_samples,),預設為 None
每個資料點的歐幾里得範數平方。
- random_state整數或 RandomState 實例,預設為 None
決定質心初始化的隨機數生成。傳遞一個整數以在多次函數呼叫中產生可重複的輸出。參見詞彙表。
- n_local_trials整數,預設為 None
每個中心(第一個除外)的種子試驗次數,其中貪婪地選擇減少慣性最多的那個。設定為 None 以使試驗次數在對數上取決於種子數量 (2+log(k)),這是建議的設定。設定為 1 會停用貪婪叢集選擇並恢復原始的 k-means++ 演算法,該演算法經實驗證明效果不如其貪婪變體。
- 返回:
- centersndarray,形狀為 (n_clusters, n_features)
k-means 的初始中心。
- indicesndarray,形狀為 (n_clusters,)
在資料陣列 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])