A from-scratch derivation and Python implementation of Principal Component Analysis — built directly from the eigenvalues and eigenvectors of the data's covariance matrix, without sklearn.decomposition.PCA.
The accompanying mathematical derivation and code walkthrough are published in Analytics Vidhya (Medium publication, 77K+ followers).
- Derivation of Principal Component Analysis (PCA) — full mathematical derivation from first principles: covariance matrix, the optimisation problem behind PCA, generalised Lagrangian, and the link to eigenvalue decomposition.
- PCA from First Principles — Code Walkthrough — implementation in plain NumPy, step by step, matching the mathematics from the first article.
PCA.ipynb— the notebook implementation:- Column standardisation (zero-centre + unit variance)
- Manual computation of the covariance matrix
- Eigendecomposition with
numpy.linalg.eig - Sorting components by eigenvalue magnitude
- Projecting data into the top-k principal components
- Numerical comparison against
sklearn.decomposition.PCAfor sanity-checking
- Lay out the data matrix
X— rows are observations, columns are features - Standardise each column (zero mean, unit variance)
- Compute the covariance matrix
S = (1/n) · Xᵀ X - Compute eigenvalues and eigenvectors of
S - Sort eigenvectors by descending eigenvalue
- Project
Xonto the top-k eigenvectors → reduced representation
pip install numpy scikit-learn matplotlib jupyter
jupyter notebook PCA.ipynbsklearn.decomposition.PCA(n_components=k) is ten lines of code. The point of this project was to make every step inside those ten lines explicit — what the covariance matrix is geometrically, why eigendecomposition appears in the solution, what eigenvalues mean as a measure of variance along each principal axis — and then verify the from-scratch result matches scikit-learn numerically.
MIT