Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

PCA

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
n_componentsint, float, 'mle' or None, default=NoneNumber of components to keep; accepts an integer, a float variance threshold, 'mle', or None.
copybool, default=TrueWhether to copy the input before fitting.
whitenbool, default=FalseWhether to whiten projected components to unit variance.
svd_solverstr, default='auto'SVD strategy: 'auto', 'full', 'covariance_eigh', 'arpack', or 'randomized'.
tolfloat, default=0.0Tolerance used by the ARPACK solver.
iterated_powerint or 'auto', default='auto'Power-iteration count for the randomized solver, or 'auto'.
n_oversamplesint, default=10Additional 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_stateint, RandomState instance or None, default=NoneSeed or random-state object controlling randomized behavior.

Returns

A fitted PCA transformer; fit_transform returns the input projected into the selected principal-component space.

Key points

  • PCA reduces dimensionality by projecting samples onto directions obtained from singular value decomposition.
  • n_components determines the retained dimensionality and may be an integer, a fraction, 'mle', or None.
  • Leaving n_components unset keeps the maximum allowable number of components.
  • The transformation subtracts each feature mean before decomposition, without scaling feature variance.
  • The full solver computes an exact SVD and can truncate the resulting components afterward.
  • The ARPACK option computes a truncated SVD directly and is intended for sparse-compatible cases.

Common mistakes

  • PCA centers features but does not standardize their scales; standardize separately when feature units or magnitudes should contribute equally.
  • The MLE component-selection mode is tied to the full solver; do not assume it works with every solver choice.

Example: Reduce Iris features with PCA

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.

When to use it

Dimensionality reduction

Compressing feature matrices before supervised learning

Low-dimensional visualization

Visualizing high-dimensional observations in two or three coordinates

Variance-preserving compression

Removing low-variance directions while preserving most variance

Interview questions

QWhat does PCA do?

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.

QHow is the number of components selected?

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.

QHow does PCA choose its SVD solver?

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.TruncatedSVD
  • sklearn.decomposition.IncrementalPCA
  • sklearn.decomposition.KernelPCA

Source: PCA — scikit-learn 1.9.1 documentation. Checked 2026-10-15.