Principal Component Analysis Cheat Sheet
A cheat sheet for Principal Component Analysis covering scikit-learn implementation, explained variance, choosing component counts, and reconstruction error.
2 PagesIntermediateMar 12, 2026
PCA with scikit-learn
Reduce dimensionality and inspect explained variance.
python
from sklearn.decomposition import PCAfrom sklearn.preprocessing import StandardScalerX_scaled = StandardScaler().fit_transform(X) # PCA is scale-sensitivepca = PCA(n_components=2)X_pca = pca.fit_transform(X_scaled)print('Explained variance ratio:', pca.explained_variance_ratio_)print('Total variance captured:', pca.explained_variance_ratio_.sum())
Choosing the Number of Components
Use a scree plot or a variance target.
python
import numpy as npimport matplotlib.pyplot as pltpca_full = PCA().fit(X_scaled)cumulative = np.cumsum(pca_full.explained_variance_ratio_)plt.plot(cumulative)plt.xlabel('Number of components'); plt.ylabel('Cumulative explained variance')# Or let sklearn pick components that explain 95% of the variancepca_95 = PCA(n_components=0.95).fit(X_scaled)print(pca_95.n_components_)
Reconstruction
Project back to the original feature space.
python
X_reduced = pca.transform(X_scaled)X_reconstructed = pca.inverse_transform(X_reduced) # lossy reconstructionreconstruction_error = ((X_scaled - X_reconstructed) ** 2).mean()
Key Concepts
Core theory behind PCA.
- Principal components- Orthogonal directions of maximum variance in the data, ordered by how much variance they explain
- Eigenvectors/eigenvalues- Components are eigenvectors of the covariance matrix; eigenvalues indicate variance captured along each
- Explained variance ratio- Fraction of total dataset variance captured by each principal component
- Dimensionality reduction- Projecting onto the top-k components reduces feature count while preserving most information
- Standardization- Features must be scaled first, or high-variance features will dominate the components
- Whitening- whiten=True rescales components to unit variance, useful before some downstream algorithms
Pro Tip
PCA components are linear combinations of all original features, which makes them hard to interpret directly — inspect pca.components_ (the loadings) to see which original features contribute most to each principal component.
Was this cheat sheet helpful?
Explore Topics
#PrincipalComponentAnalysis#PrincipalComponentAnalysisCheatSheet#DataScience#Intermediate#PCAWithScikitLearn#ChoosingTheNumberOfComponents#Reconstruction#KeyConcepts#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance