Implementation:Scikit learn Scikit learn IncrementalPCA
| Knowledge Sources | |
|---|---|
| Domains | Dimensionality Reduction, Online Learning |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for memory-efficient incremental principal components analysis provided by scikit-learn.
Description
IncrementalPCA performs linear dimensionality reduction using Singular Value Decomposition of the data in mini-batches, keeping only the most significant singular vectors. Unlike standard PCA, it has constant memory complexity on the order of batch_size times n_features, enabling use with np.memmap files and sparse input without loading the entire dataset into memory. The data is centered but not scaled before applying the SVD.
Usage
Use IncrementalPCA when your dataset is too large to fit in memory for standard PCA, or when data arrives in a streaming fashion. It is particularly well suited for out-of-core PCA on large datasets, processing data from memmap files, and scenarios requiring partial_fit for online learning.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/decomposition/_incremental_pca.py
Signature
class IncrementalPCA(_BasePCA):
def __init__(
self,
n_components=None,
*,
whiten=False,
copy=True,
batch_size=None,
):
Import
from sklearn.decomposition import IncrementalPCA
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_components | int | No | Number of components to keep. Defaults to min(n_samples, n_features) if None. |
| whiten | bool | No | When True, component vectors are scaled to ensure uncorrelated outputs with unit variance (default=False). |
| copy | bool | No | If False, X will be overwritten (default=True). |
| batch_size | int | No | Number of samples per batch. Defaults to 5 * n_features if None. |
Outputs
| Name | Type | Description |
|---|---|---|
| components_ | ndarray of shape (n_components, n_features) | Principal axes in feature space (directions of maximum variance). |
| explained_variance_ | ndarray of shape (n_components,) | Variance explained by each selected component. |
| explained_variance_ratio_ | ndarray of shape (n_components,) | Proportion of variance explained by each selected component. |
| singular_values_ | ndarray of shape (n_components,) | Singular values corresponding to each selected component. |
| mean_ | ndarray of shape (n_features,) | Per-feature empirical mean, estimated from the training set. |
| n_samples_seen_ | int | Total number of samples processed by the estimator. |
Usage Examples
Basic Usage
import numpy as np
from sklearn.decomposition import IncrementalPCA
X = np.random.rand(1000, 50)
ipca = IncrementalPCA(n_components=10, batch_size=200)
X_transformed = ipca.fit_transform(X)
print(X_transformed.shape) # (1000, 10)
# Alternatively, use partial_fit for streaming data
ipca2 = IncrementalPCA(n_components=10)
for batch in np.array_split(X, 5):
ipca2.partial_fit(batch)
X_transformed2 = ipca2.transform(X)
print(X_transformed2.shape) # (1000, 10)