IncrementalPCA
¶
-
class
ibex.sklearn.decomposition.
IncrementalPCA
(n_components=None, whiten=False, copy=True, batch_size=None)¶ Bases:
sklearn.decomposition.incremental_pca.IncrementalPCA
,ibex._base.FrameMixin
Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Note
The documentation following is of the original class wrapped by this class. This class wraps the attribute
components_
.Example:
>>> import pandas as pd >>> import numpy as np >>> from ibex.sklearn import datasets >>> from ibex.sklearn.decomposition import PCA as PdPCA
>>> iris = datasets.load_iris() >>> features = iris['feature_names'] >>> iris = pd.DataFrame( ... np.c_[iris['data'], iris['target']], ... columns=features+['class'])
>>> iris[features] sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) 0 5.1 3.5 1.4 0.2 1 4.9 3.0 1.4 0.2 2 4.7 3.2 1.3 0.2 3 4.6 3.1 1.5 0.2 4 5.0 3.6 1.4 0.2 ...
>>> PdPCA(n_components=2).fit(iris[features], iris['class']).transform(iris[features]) comp_0 comp_1 0 -2.684207 ...0.326607 1 -2.715391 ...0.169557 2 -2.889820 ...0.137346 3 -2.746437 ...0.311124 4 -2.728593 ...0.333925 ...
Incremental principal components analysis (IPCA).
Linear dimensionality reduction using Singular Value Decomposition of centered data, keeping only the most significant singular vectors to project the data to a lower dimensional space.
Depending on the size of the input data, this algorithm can be much more memory efficient than a PCA.
This algorithm has constant memory complexity, on the order of
batch_size
, enabling use of np.memmap files without loading the entire file into memory.The computational overhead of each SVD is
O(batch_size * n_features ** 2)
, but only 2 * batch_size samples remain in memory at a time. There will ben_samples / batch_size
SVD computations to get the principal components, versus 1 large SVD of complexityO(n_samples * n_features ** 2)
for PCA.Read more in the User Guide.
- n_components : int or None, (default=None)
- Number of components to keep. If
n_components `` is ``None
, thenn_components
is set tomin(n_samples, n_features)
. - whiten : bool, optional
When True (False by default) the
components_
vectors are divided byn_samples
timescomponents_
to ensure uncorrelated outputs with unit component-wise variances.Whitening will remove some information from the transformed signal (the relative variance scales of the components) but can sometimes improve the predictive accuracy of the downstream estimators by making data respect some hard-wired assumptions.
- copy : bool, (default=True)
- If False, X will be overwritten.
copy=False
can be used to save memory but is unsafe for general use. - batch_size : int or None, (default=None)
- The number of samples to use for each batch. Only used when calling
fit
. Ifbatch_size
isNone
, thenbatch_size
is inferred from the data and set to5 * n_features
, to provide a balance between approximation accuracy and memory consumption.
- components_ : array, shape (n_components, n_features)
- Components with maximum variance.
- explained_variance_ : array, shape (n_components,)
- Variance explained by each of the selected components.
- explained_variance_ratio_ : array, shape (n_components,)
- Percentage of variance explained by each of the selected components. If all components are stored, the sum of explained variances is equal to 1.0.
- singular_values_ : array, shape (n_components,)
- The singular values corresponding to each of the selected components.
The singular values are equal to the 2-norms of the
n_components
variables in the lower-dimensional space. - mean_ : array, shape (n_features,)
- Per-feature empirical mean, aggregate over calls to
partial_fit
. - var_ : array, shape (n_features,)
- Per-feature empirical variance, aggregate over calls to
partial_fit
. - noise_variance_ : float
- The estimated noise covariance following the Probabilistic PCA model from Tipping and Bishop 1999. See “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
- The estimated number of components. Relevant when
n_components=None
. - n_samples_seen_ : int
- The number of samples processed by the estimator. Will be reset on
new calls to fit, but increments across
partial_fit
calls.
Implements the incremental PCA model from: 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. See http://www.cs.toronto.edu/~dross/ivt/RossLimLinYang_ijcv.pdf
This model is an extension of the Sequential Karhunen-Loeve Transform from: 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 http://www.cs.technion.ac.il/~mic/doc/skl-ip.pdf
We have specifically abstained from an optimization used by authors of both papers, a QR decomposition used in specific situations to reduce the algorithmic complexity of the SVD. The source for this technique is Matrix Computations, Third Edition, G. Holub and C. Van Loan, Chapter 5, section 5.4.4, pp 252-253.. This technique has been omitted because it is advantageous only when decomposing a matrix with
n_samples
(rows) >= 5/3 *n_features
(columns), and hurts the readability of the implemented algorithm. This would be a good opportunity for future optimization, if it is deemed necessary.- 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.
- Golub and C. Van Loan. Matrix Computations, Third Edition, Chapter 5,
- Section 5.4.4, pp. 252-253.
PCA RandomizedPCA KernelPCA SparsePCA TruncatedSVD
-
fit
(X, y=None)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Fit the model with X, using minibatches of size batch_size.
- X : array-like, shape (n_samples, n_features)
- Training data, where n_samples is the number of samples and n_features is the number of features.
y : Ignored.
- self : object
- Returns the instance itself.
- A parameter
-
fit_transform
(X, y=None, **fit_params)¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- X : numpy array of shape [n_samples, n_features]
- Training set.
- y : numpy array of shape [n_samples]
- Target values.
- X_new : numpy array of shape [n_samples, n_features_new]
- Transformed array.
- A parameter
-
inverse_transform
(X)¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Transform data back to its original space.
In other words, return an input X_original whose transform would be X.
- X : array-like, shape (n_samples, n_components)
- New data, where n_samples is the number of samples and n_components is the number of components.
X_original array-like, shape (n_samples, n_features)
If whitening is enabled, inverse_transform will compute the exact inverse operation, which includes reversing whitening.
- A parameter
-
partial_fit
(X, y=None, check_input=True)[source]¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Incremental fit with X. All of X is processed as a single batch.
- X : array-like, shape (n_samples, n_features)
- Training data, where n_samples is the number of samples and n_features is the number of features.
- check_input : bool
- Run check_array on X.
y : Ignored.
- self : object
- Returns the instance itself.
- A parameter
-
transform
(X)¶ Note
The documentation following is of the class wrapped by this class. There are some changes, in particular:
- A parameter
X
denotes apandas.DataFrame
. - A parameter
y
denotes apandas.Series
.
Apply dimensionality reduction to X.
X is projected on the first principal components previously extracted from a training set.
- X : array-like, shape (n_samples, n_features)
- New data, where n_samples is the number of samples and n_features is the number of 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, copy=True, n_components=2, whiten=False) >>> ipca.transform(X)
- A parameter
- A parameter