用法:
class cuml.IncrementalPCA(*, handle=None, n_components=None, whiten=False, copy=True, batch_size=None, verbose=False, output_type=None)
基於來自scikit-learn 0.23.1 的sklearn.decomposition IncrementalPCA
增量主成分分析(IPCA)。使用數據的奇異值分解進行線性降維,僅保留最重要的奇異向量以將數據投影到較低維空間。在應用 SVD 之前,輸入數據居中,但未針對每個特征進行縮放。根據輸入數據的大小,該算法的內存效率可能比 PCA 高得多,並且允許稀疏輸入。該算法具有恒定的內存複雜度,大約為
batch_size * n_features
,可以在不將整個文件加載到內存的情況下使用 np.memmap 文件。對於稀疏矩陣,將輸入分批轉換為密集矩陣(以便能夠減去均值),從而避免在任何時候存儲整個密集矩陣。每個 SVD 的計算開銷為O(batch_size * n_features ** 2)
,但一次隻有 2 * batch_size 樣本保留在內存中。將有n_samples / batch_size
SVD 計算來獲得主成分,而 PCA 需要 1 個複雜的大 SVDO(n_samples * n_features ** 2)
。- handle:cuml.Handle
指定 cuml.handle 保存用於此模型中計算的內部 CUDA 狀態。最重要的是,這指定了將用於模型計算的 CUDA 流,因此用戶可以通過在多個流中創建句柄在不同的流中同時運行不同的模型。如果為 None,則創建一個新的。
- n_components:int 或無,(默認=無)
要保留的組件數。如果
n_components
是None
,則n_components
設置為min(n_samples, n_features)
。- whiten:布爾型,可選
如果為真,de-correlates 組件。這是通過將它們除以相應的奇異值然後乘以 sqrt(n_samples) 來完成的。白化允許每個組件具有單位方差並刪除multi-collinearity。它可能對像LinearRegression 這樣的相關特征會導致問題的下遊任務有益。
- copy:布爾,(默認=真)
如果為 False,X 將被覆蓋。
copy=False
可用於節省內存,但一般使用不安全。- batch_size:int 或無,(默認=無)
每批要使用的樣本數。僅在調用
fit
時使用。如果batch_size
是None
,則從數據中推斷出batch_size
並將其設置為5 * n_features
,以在近似精度和內存消耗之間取得平衡。- verbose:int 或布爾值,默認=False
設置日誌記錄級別。它必須是
cuml.common.logger.level_*
之一。有關詳細信息,請參閱詳細級別。- output_type:{‘input’, ‘cudf’, ‘cupy’, ‘numpy’, ‘numba’},默認=無
用於控製估計器的結果和屬性的輸出類型的變量。如果為 None,它將繼承在模塊級別設置的輸出類型
cuml.global_settings.output_type
。有關詳細信息,請參閱輸出數據類型配置。
參數:
注意:
從 [1] 實現增量 PCA 模型。此模型是來自 [2] 的順序 Karhunen-Loeve 變換的擴展。我們特別放棄了兩篇論文的作者使用的優化,在特定情況下使用 QR 分解來降低 SVD 的算法複雜性。該技術的來源是 [3] 。此技術已被省略,因為它僅在使用
n_samples >= 5/3 * n_features
分解矩陣時才有優勢,其中n_samples
和n_features
分別是矩陣行和列。此外,它損害了所實現算法的可讀性。如果認為有必要,這將是未來優化的好機會。參考:
- 1
- 2
- 3
G. Golub and C. Van Loan. Matrix Computations, Third Edition, Chapter 5, Section 5.4.4, pp. 252-253.
- 4
C. Bishop, 1999. “Pattern Recognition and Machine Learning”, Section 12.2.1, pp. 574
例子:
>>> from cuml.decomposition import IncrementalPCA >>> import cupy as cp >>> import cupyx >>> >>> X = cupyx.scipy.sparse.random(1000, 4, format='csr', density=0.07) >>> ipca = IncrementalPCA(n_components=2, batch_size=200) >>> ipca.fit(X) >>> >>> # Components: >>> ipca.components_ array([[-0.02362926, 0.87328851, -0.15971988, 0.45967206], [-0.14643883, 0.11414225, 0.97589354, 0.11471273]]) >>> >>> # Singular Values: >>> ipca.singular_values_ array([4.90298662, 4.54498226]) >>> >>> # Explained Variance: >>> ipca.explained_variance_ array([0.02406334, 0.02067754]) >>> >>> # Explained Variance Ratio: >>> ipca.explained_variance_ratio_ array([0.28018011, 0.24075775]) >>> >>> # Mean: >>> ipca.mean_ array([0.03249896, 0.03629852, 0.03268694, 0.03216601]) >>> >>> # Noise Variance: >>> ipca.noise_variance_.item() 0.003474966583315544
- components_:數組,形狀(n_components,n_features)
具有最大方差的組件。
- explained_variance_:數組,形狀(n_components,)
每個選定組件解釋的方差。
- explained_variance_ratio_:數組,形狀(n_components,)
每個選定組件解釋的方差百分比。如果存儲了所有分量,則解釋方差之和等於 1.0。
- singular_values_:數組,形狀(n_components,)
對應於每個選定組件的奇異值。奇異值等於 lower-dimensional 空間中
n_components
變量的 2 範數。- mean_:數組,形狀(n_features,)
Per-feature 經驗平均值,匯總對
partial_fit
的調用。- var_:數組,形狀(n_features,)
Per-feature 經驗方差,匯總調用
partial_fit
。- noise_variance_:浮點數
遵循來自 [4] 的概率 PCA 模型的估計噪聲協方差。
- n_components_:int
組件的估計數量。相關時
n_components=None
。- n_samples_seen_:int
估計器處理的樣本數。將在新調用時重置以適應,但在
partial_fit
調用中遞增。- batch_size_:int
從
batch_size
推斷的批量大小。
屬性:
相關用法
- 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.IncrementalPCA。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。