用法:
class cuml.KMeans(*, handle=None, n_clusters=8, max_iter=300, tol=0.0001, verbose=False, random_state=1, init='scalable-k-means++', n_init=1, oversampling_factor=2.0, max_samples_per_batch=32768, output_type=None)
KMeans 是一種基本但函數強大的聚類方法,通過期望最大化進行了優化。它隨機選擇 X 中的 K 個數據點,並計算哪些樣本接近這些點。對於每個點簇,都會計算一個平均值(因此得名),這成為新的質心。
CUML的keans期望array-like對象或cudfDataFrame並支持可縮放的瀏覽量++初始化方法。該方法比隨機選擇k點更穩定。
- handle:cuml.Handle
指定 cuml.handle 保存用於此模型中計算的內部 CUDA 狀態。最重要的是,這指定了將用於模型計算的 CUDA 流,因此用戶可以通過在多個流中創建句柄在不同的流中同時運行不同的模型。如果為 None,則創建一個新的。
- n_clusters:int(默認值 = 8)
您想要的質心或集群的數量。
- max_iter:int(默認值 = 300)
EM 的迭代次數越多,精度越高,但速度越慢。
- tol:float64(默認 = 1e-4)
質心平均值變化不大時的停止標準。
- verbose:int 或布爾值,默認=False
設置日誌記錄級別。它必須是
cuml.common.logger.level_*
之一。有關詳細信息,請參閱詳細級別。- random_state:int(默認值 = 1)
如果您希望重新啟動 Python 時結果相同,請選擇一個狀態。
- init:‘scalable-kmeans++’、‘k-means||’、‘random’ 或 ndarray(默認 = ‘scalable-k-means++’)#noqa
‘scalable-k-means++’或‘k-means||’:使用快速穩定的可擴展kmeans++初始化。 ‘random’:從初始質心的數據中隨機選擇 ‘n_cluster’ 觀測值(行)。如果傳遞了一個 ndarray,它的形狀應該是 (n_clusters, n_features) 並給出初始中心。
- n_init: int (default = 1):
k-means 算法將使用不同種子調用的實例數。最終結果將來自 n_init 實例中產生最低慣性的實例。
- oversampling_factor:float64(默認 = 2.0)
在可擴展的k-means++ 初始化中為潛在質心采樣的點數。增加此值可以以內存為代價獲得更好的初始質心。在可擴展的k-means++ 中采樣的質心總數為oversampling_factor * n_clusters * 8。
- max_samples_per_batch:int(默認值 = 32768)
用於成對距離計算批次的數據樣本數。該計算在兩個擬合預測中完成。默認值應該適合大多數情況。批量成對距離計算中的元素總數為max_samples_per_batch * n_clusters。當 n_clusters 變得非常大時,可能有必要降低此數字。
- output_type:{‘input’, ‘cudf’, ‘cupy’, ‘numpy’, ‘numba’},默認=無
用於控製估計器的結果和屬性的輸出類型的變量。如果為 None,它將繼承在模塊級別設置的輸出類型
cuml.global_settings.output_type
。有關詳細信息,請參閱輸出數據類型配置。
參數:
注意:
KMeans 需要指定 n_clusters。這意味著需要大致猜測或知道一個數據集有多少個集群。如果不確定,可以從少量集群開始,並使用 PCA、UMAP 或T-SNE 可視化生成的集群,並驗證它們看起來是否合適。
KMeans 的應用
KMeans 的最大優勢是它的速度和簡單性。這就是為什麽 KMeans 是許多從業者聚類算法的首選。 KMeans 已廣泛用於聚類數量大致已知的情況,例如大數據聚類任務、圖像分割和醫學聚類。
有關其他文檔,請參閱 scikitlearn’s Kmeans 。
例子:
# Both import methods supported from cuml import KMeans from cuml.cluster import KMeans import cudf import numpy as np import pandas as pd def np2cudf(df): # convert numpy array to cuDF dataframe df = pd.DataFrame({'fea%d'%i:df[:,i] for i in range(df.shape[1])}) pdf = cudf.DataFrame() for c,column in enumerate(df): pdf[str(c)] = df[column] return pdf a = np.asarray([[1.0, 1.0], [1.0, 2.0], [3.0, 2.0], [4.0, 3.0]], dtype=np.float32) b = np2cudf(a) print("input:") print(b) print("Calling fit") kmeans_float = KMeans(n_clusters=2) kmeans_float.fit(b) print("labels:") print(kmeans_float.labels_) print("cluster_centers:") print(kmeans_float.cluster_centers_)
輸出:
input: 0 1 0 1.0 1.0 1 1.0 2.0 2 3.0 2.0 3 4.0 3.0 Calling fit labels: 0 0 1 0 2 1 3 1 cluster_centers: 0 1 0 1.0 1.5 1 3.5 2.5
- cluster_centers_:數組
最終集群的坐標。這表示每個數據集群的“mean”。
- labels_:數組
每個數據點屬於哪個集群。
屬性:
相關用法
- Python cuml.metrics.pairwise_distances.pairwise_distances用法及代碼示例
- Python cuml.neighbors.KNeighborsClassifier用法及代碼示例
- Python cuml.ensemble.RandomForestRegressor用法及代碼示例
- Python cuml.svm.SVC用法及代碼示例
- Python cuml.svm.SVR用法及代碼示例
- Python cuml.Lasso用法及代碼示例
- Python cuml.tsa.ARIMA.predict用法及代碼示例
- Python cuml.multiclass.OneVsRestClassifier用法及代碼示例
- Python cuml.preprocessing.LabelBinarizer用法及代碼示例
- Python cuml.random_projection.GaussianRandomProjection用法及代碼示例
- Python cuml.MBSGDRegressor用法及代碼示例
- Python cuml.experimental.preprocessing.PolynomialFeatures用法及代碼示例
- Python cuml.PCA用法及代碼示例
- Python cuml.feature_extraction.text.HashingVectorizer用法及代碼示例
- Python cuml.DBSCAN用法及代碼示例
- Python cuml.dask.feature_extraction.text.TfidfTransformer用法及代碼示例
- Python cuml.TruncatedSVD用法及代碼示例
- Python cuml.common.memory_utils.using_output_type用法及代碼示例
- Python cuml.preprocessing.text.stem.PorterStemmer用法及代碼示例
- Python cuml.experimental.preprocessing.add_dummy_feature用法及代碼示例
注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cuml.KMeans。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。