Principal Component Analysis and Factor Extraction › Using scikit-learn for PCA

Michael BrenndoerferDecember 12, 20252 min read
Quantitative FinanceAll chapters
Part 8 of 15Econometrics and Statistical Modeling
Chapter 44Principal Component Analysis and Factor Extraction › Using scikit-learn for PCA
Reading section 7 of 13 · Using scikit-learn for PCA · 2 minSection 7 of 13 · 2 min

Book contents

Section 7 of 13 · 2 min read

Principal Component Analysis and Factor Extraction › Using scikit-learn for PCAOne section · Sections in this chapter7 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

For production applications, scikit-learn provides an efficient and well-tested PCA implementation:

In[27]:
Code
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# Standardize the data (optional but often recommended for equity returns)
scaler = StandardScaler()
returns_standardized = scaler.fit_transform(returns)

# Fit PCA
pca = PCA()
pca.fit(returns_standardized)

# Get results
explained_variance_ratio = pca.explained_variance_ratio_
cumulative_variance_ratio = np.cumsum(explained_variance_ratio)
loadings_sklearn = (
    pca.components_.T
)  # Transpose to get (n_features, n_components)
pc_scores_sklearn = pca.transform(returns_standardized)
Out[28]:
Console
PCA Results (scikit-learn with standardized data):
--------------------------------------------------
PC1:  62.22%  (Cumulative:  62.22%)
PC2:   7.67%  (Cumulative:  69.89%)
PC3:   5.23%  (Cumulative:  75.12%)
PC4:   4.70%  (Cumulative:  79.82%)
PC5:   3.90%  (Cumulative:  83.72%)

The output confirms that the first few principal components capture the majority of the variance, consistent with our manual implementation. However, the exact percentages differ slightly because we standardized the data. Standardization effectively performs PCA on the correlation matrix (giving equal weight to all assets) rather than the covariance matrix (where high-volatility assets dominate).

Standardization in PCA

When variables have different scales, standardizing them (subtracting the mean and dividing by standard deviation) before PCA ensures that all variables contribute equally to the analysis. For yield curves, where all variables are in the same units (percentage yields), standardization is typically unnecessary. For equity returns with different volatilities, standardization may be appropriate depending on whether you want to explain variance in original units or give equal weight to all assets.