Principal Component Analysis and Factor Extraction › Using scikit-learn for PCA
Quantitative FinanceAll chapters 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:
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)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).
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.