Dimensionality reduction
Compressing feature matrices before supervised learning
Scikit-Learn · Class
PCA performs linear dimensionality reduction by centering data and using singular value decomposition to project it into a lower-dimensional space. It supports exact, randomized, covariance-based, and truncated SVD strategies.
PCA learns orthogonal directions from centered input data using singular value decomposition, then projects observations onto the selected directions. Depending on the data shape and solver settings, it can use exact, covariance-based, randomized, or truncated SVD methods.
class sklearn.decomposition.PCA(n_components=None, *, copy=True, whiten=False, svd_solver='auto', tol=0.0, iterated_power='auto', n_oversamples=10, power_iteration_normalizer='auto', random_state=None)| Name | Type | Description |
|---|---|---|
n_components | int, float, 'mle' or None, default=None | Number of components to keep; accepts an integer, a float variance threshold, 'mle', or None. |
copy | bool, default=True | Whether to copy the input before fitting. |
whiten | bool, default=False | Whether to whiten projected components to unit variance. |
svd_solver | str, default='auto' | SVD strategy: 'auto', 'full', 'covariance_eigh', 'arpack', or 'randomized'. |
tol | float, default=0.0 | Tolerance used by the ARPACK solver. |
iterated_power | int or 'auto', default='auto' | Power-iteration count for the randomized solver, or 'auto'. |
n_oversamples | int, default=10 | Additional random vectors used by the randomized solver. |
power_iteration_normalizer | {'auto', 'QR', 'LU', 'none'}, default='auto' | Normalization method for randomized power iterations, or 'auto'. |
random_state | int, RandomState instance or None, default=None | Seed or random-state object controlling randomized behavior. |
A fitted PCA transformer; fit_transform returns the input projected into the selected principal-component space.
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
X, _ = load_iris(return_X_y=True)
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print(X_reduced.shape)
print(pca.explained_variance_ratio_.sum())Fits PCA to the Iris features, projects them onto two principal components, and reports the reduced shape and retained variance.
Compressing feature matrices before supervised learning
Visualizing high-dimensional observations in two or three coordinates
Removing low-variance directions while preserving most variance
PCA is a linear dimensionality-reduction transformer. It centers the data, computes singular vectors with an SVD-based method, and projects samples into the retained component space.
Follow-up: How would you preserve a target amount of variance? Set n_components to a float between 0 and 1 when using the full solver; PCA retains enough components to reach that variance fraction.
n_components controls how many components are retained. It accepts an integer, a variance fraction, 'mle', or None; None keeps min(n_samples, n_features) components.
Follow-up: What does a float mean? With the full solver, it specifies the minimum cumulative explained-variance proportion to retain.
The auto solver chooses an approach based on input shape and requested dimensionality. Available strategies include full SVD, covariance_eigh, randomized SVD, and ARPACK truncated SVD.
Follow-up: When is covariance_eigh used? It is selected for suitable tall data and computes an eigen-decomposition of the centered covariance matrix, trading memory for speed.
sklearn.decomposition.TruncatedSVDsklearn.decomposition.IncrementalPCAsklearn.decomposition.KernelPCA