sklearn.decomposition.IncrementalPCA?
class sklearn.decomposition.IncrementalPCA(n_components=None, *, whiten=False, copy=True, batch_size=None)
增量主成分分析(IPCA)。
利用奇異值分解對數據進行線性降維,只保留最重要的奇異向量將數據投影到較低維空間。在應用SVD之前,輸入數據是居中的,但沒有針對每個特征進行縮放。
根據輸入數據的大小,該算法比PCA的內存效率更高,并且允許稀疏輸入。
該算法具有不變的內存復雜度,大小依次為 batch_size * n_features
,可以使用np.memmap
文件,而不加載整個文件到內存。對于稀疏矩陣,輸入被批量轉換為密集矩陣(以便能夠減去平均值),避免在任何時候存儲整個密集矩陣。
每個SVD的計算代價是 O(batch_size * n_features ** 2)
,但是每次只有2 * batch_size
樣本保留在內存中。為了得到主成分,需要進行n_samples / batch_size
SVD計算,而對于主成分分析,需要1個大的SVD復雜度O(n_samples * n_features ** 2)
。
在用戶指南中閱讀更多內容
新版本0.16。
參數 | 說明 |
---|---|
n_components | int or None, (default=None) 要保存的組件數量。如果 n_components ' 'is' ' None ,那么n_components 被設置為min(n_samples, n_features) 。 |
whiten | bool, optional 當為真(默認為假)時, components_ 向量除以n_samples 乘以components_ 以確保輸出與單位樣本方差不相關。白化將從轉換信號中去除一些信息(樣本的相對方差尺度),但有時可以通過使數據尊重一些硬連接的假設來提高下游估計器的預測精度。 |
copy | bool, (default=True) 如果為假,X將被覆蓋。 copy=False 可以用于節省內存,但一般使用不安全。 |
batch_size | int or None, (default=None) 每批使用的樣品數量。只在使用fit時使用。如果 batch_size 為None ,則從數據中推斷出batch_size 并設置為5 * n_features ,以在近似精度和內存消耗之間提供平衡。 |
屬性 | 說明 |
---|---|
components_ | array, shape (n_components, n_features) 方差最大的分量。 |
explained_variance_ | array, shape (n_components,) 方差由每個選擇的樣本部分解釋。 |
explained_variance_ratio_ | array, shape (n_components,) 所選擇的每個樣本所解釋的方差百分比。如果所有樣本都被存儲,則已解釋方差之和為1.0。 |
singular_values_ | array, shape (n_components,) 對應于每個選定分量的奇異值。奇異值等于低維空間中n_component變量的2-范數。 |
mean_ | array, shape (n_features,) 每個特性的經驗平均值,通過調用partial_fit進行聚合 |
var_ | array, shape (n_features,) 每個特性的經驗方差,通過調用partial_fit進行聚合。 |
noise_variance_ | float 根據Tipping和Bishop 1999年的概率PCA模型估計的噪聲協方差。參見“Pattern Recognition and Machine Learning” by C. Bishop, 12.2.1 p. 574 or http://www.miketipping.com/papers/met-mppca.pdf. |
n_components_ | int 估計的樣本數量。有關當n_components =None。 |
n_samples_seen_ | int 估計器處理的樣本數目。將在新調用時重置fit,但在 partial_fit 調用時增加。 |
batch_size_ | int 從 batch_size 推斷批大小。 |
另見
筆記
實現PCA增量模型,由: D. Ross, J. Lim, R. Lin, M. Yang, Incremental Learning for Robust Visual Tracking, International Journal of Computer Vision, Volume 77, Issue 1-3, pp. 125-141, May 2008. 參見https://www.cs.toronto.edu/~dross/ivt/RossLimLinYang_ijcv.pdf
該模型是由:A. Levy and M. Lindenbaum, Sequential Karhunen-Loeve Basis Extraction and its Application to Images, IEEE Transactions on Image Processing, Volume 9, Number 8, pp. 1371-1374, August 2000. See https://www.cs.technion.ac.il/~mic/doc/skl-ip.pdf
我們特意放棄了兩篇論文作者使用的一種優化方法,即在特定情況下使用的QR分解來降低奇異值分解的算法復雜度。這種技術的來源是 Matrix Computations, Third Edition, G. Holub and C. Van Loan, Chapter 5, section 5.4.4, pp 252-253.。這里省略了這項技術,因為它僅在使用n_samples (rows) >= 5/3 * n_features (columns)
分解矩陣時才具有優勢,而且會損害已實現算法的可讀性。如果認為有必要,這將是未來優化的好機會。
參考資料:
D. Ross, J. Lim, R. Lin, M. Yang. Incremental Learning for Robust Visual Tracking, International Journal of Computer Vision, Volume 77, Issue 1-3, pp. 125-141, May 2008.
G. Golub and C. Van Loan. Matrix Computations, Third Edition, Chapter 5, Section 5.4.4, pp. 252-253.
示例:
>>> from sklearn.datasets import load_digits
>>> from sklearn.decomposition import IncrementalPCA
>>> from scipy import sparse
>>> X, _ = load_digits(return_X_y=True)
>>> transformer = IncrementalPCA(n_components=7, batch_size=200)
>>> # either partially fit on smaller batches of data
>>> transformer.partial_fit(X[:100, :])
IncrementalPCA(batch_size=200, n_components=7)
>>> # or let the fit function itself divide the data into batches
>>> X_sparse = sparse.csr_matrix(X)
>>> X_transformed = transformer.fit_transform(X_sparse)
>>> X_transformed.shape
(1797, 7)
方法
方法 | 說明 |
---|---|
fit (self, X[, y]) |
使用小批量batch_size,使模型與X匹配。 |
fit_transform (self, X[, y]) |
擬合數據,然后轉換它。 |
get_covariance (self) |
用生成模型計算數據協方差。 |
get_params (self[, deep]) |
獲取這個估計器的參數。 |
get_precision (self) |
利用生成模型計算數據精度矩陣。 |
inverse_transform (self, X) |
將數據轉換回其原始空間。 |
partial_fit (self, X[, y, check_input]) |
增量擬合。 |
set_params (self, **params) |
設置這個估計器的參數。 |
transform (self, X) |
對X應用維數約簡。 |
__init__(self, n_components=None, *, whiten=False, copy=True, batch_size=None)
初始化self. See 請參閱help(type(self))以獲得準確的說明。
fit(self, X, y=None)
使用小批量batch_size,使模型與X匹配。
參數 | 說明 |
---|---|
X | **array-like or sparse matrix, shape (n_samples, n_features)**訓練數據,其中n_samples為樣本數量,n_features為特征數量。 |
y | Ignored |
返回值 | 說明 |
---|---|
self | object 返回距離自身 |
fit_transform(self, X, y=None, *fit_params)
擬合數據,然后轉換它。
使用可選參數fit_params將transformer與X和y匹配,并返回X的轉換版本。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
Y | ndarray of shape (n_samples,), default=None 目標值 |
**fit_params | dict : 其他擬合參數。 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 轉換數組 |
get_covariance(self)
用生成模型計算數據協方差。
cov = components_.T * S**2 * components_ + sigma2 * eye(n_features)
其中 S**2 包含解釋方差, sigma2 包含解釋方差
返回值 | 說明 |
---|---|
cov | shape=(n_features, n_features) 數據的估計協方差。 |
get_params(self, deep=True)
獲取這個估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為真,將返回此估計器的參數以及包含的作為估計器的子對象。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到它們的值。。 |
get_precision(self)
[源碼]利用生成模型計算數據精度矩陣。
等于協方差的逆,但為了效率,用矩陣逆引理計算。
返回值 | 說明 |
---|---|
precision | array, shape=(n_features, n_features) 數據的估計精度。。 |
inverse_transform(self, X)
將數據轉換回其原始空間。
換句話說,返回一個轉換為X的輸入X_original
。
參數 | 說明 |
---|---|
X | array-like, shape (n_samples, n_components) 新數據,其中n_samples是樣本數量,n_components是組件數量。 |
返回值 | X_original array-like, shape (n_samples, n_features) |
筆記
如果啟用了白化,inverse_transform將計算精確的反運算,其中包括反向白化。
partial_fit(self, X, y=None, check_input=True)
增量擬合。所有的X都作為一個batch。
參數 | 說明 |
---|---|
X | array-like, shape (n_samples, n_features) 訓練數據,其中n_samples為樣本數量,n_features為特征數量。 |
check_input | bool 在X上運行check_array。 |
y | Ignored |
返回值 | 說明 |
---|---|
self | object 返回實例本身。 |
set_params(self, **params)
設置這個估計器的參數。
該方法適用于簡單估計器和嵌套對象(如pipelines)。后者具有形式為_
參數 | 說明 |
---|---|
**params | dict 參數估計器。 |
返回值 | 說明 |
---|---|
self | object 實例估計。 |
transform(self, X)
對X應用維數約簡。
X被投影到之前從訓練集中提取的第一個主成分上,如果X是稀疏的,則使用size為minibatches的batch_size。
參數 | 說明 |
---|---|
X | **array-like, shape (n_samples, n_features)**** 新數據,其中n_samples為樣本數量,n_features為特征數量。 |
返回值 | 說明 |
---|---|
X_new | array-like, shape (n_samples, n_components) |
>>> import numpy as np
>>> from sklearn.decomposition import IncrementalPCA
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2],
... [1, 1], [2, 1], [3, 2]])
>>> ipca = IncrementalPCA(n_components=2, batch_size=3)
>>> ipca.fit(X)
IncrementalPCA(batch_size=3, n_components=2)
>>> ipca.transform(X) # doctest: +SKIP