PCA Implementation in scikit-learn
What You'll Learn
You'll learn how to use scikit-learn's PCA class to perform dimensionality reduction by fitting, transforming data, and extracting components and variance information.
The Core Workflow
Now that you understand PCA theory, scikit-learn makes implementation straightforward with the PCA class from sklearn.decomposition. The workflow follows the familiar fit-transform pattern you've seen with other sklearn models.
Creating and Fitting PCA
First, create a PCA object specifying how many components you want:
from sklearn.decomposition import PCA
# Create PCA with 2 components
pca = PCA(n_components=2)
# Fit to your standardized data
pca.fit(X_standardized)
The fit() method computes the covariance matrix, performs eigendecomposition, and identifies the principal components.
Transforming Data
Transform your original data into the new coordinate system:
# Transform to reduced dimensions
X_pca = pca.transform(X_standardized)
# Or fit and transform in one step
X_pca = pca.fit_transform(X_standardized)
Accessing Results
After fitting, you can inspect what PCA learned:
# The principal components (eigenvectors)
components = pca.components_ # shape: (n_components, n_features)
# Explained variance by each component
explained_var = pca.explained_variance_
# Explained variance ratio (proportion of total variance)
explained_var_ratio = pca.explained_variance_ratio_
# Cumulative explained variance
import numpy as np
cumulative_var = np.cumsum(explained_var_ratio)
The explained_variance_ratio_ is particularly useful for determining how much information each component retains—values closer to 1.0 mean that component captures most of the original variance.
Key Takeaway: Scikit-learn's PCA class provides a simple fit-transform interface to reduce dimensionality, with components_ giving you the new axes and explained_variance_ratio_ showing how much information each component preserves.