Unsupervised Learning and PCA
This lecture shifts from supervised to unsupervised learning — methods that find structure without a target variable. We cover principal component analysis (PCA) for dimensionality reduction: how components are constructed — including why they are the eigenvectors of the covariance matrix — what explained-variance ratios tell you, and why scaling matters.
Re-run the worked example
Download the complete Python example and its pinned requirements into the same folder. Wine and digits are included with scikit-learn; no data download is needed. The published numbers were verified with Python 3.14.7 and scikit-learn 1.9.0.
In a fresh Python environment, run:
python -m pip install -r requirements-lecture-09.txt
python lecture_09_unsupervised.pyThe script starts with descriptive PCA on wine, then splits digits into training
and test data. It compares PCA candidates and a no-PCA baseline using training
CV, applies the declared one-SE heuristic, and refits before reporting any test
scores. The rule selects 50 components: CV accuracy 0.968, test accuracy 0.967.
The maximum-CV candidate keeps all 64 components and matches the no-PCA baseline
at CV 0.970 and test 0.972. The one-SE tolerance does not establish statistical
equivalence, and these predeclared test reports do not select or retune a model.
To save the training-data projection coloured by digit label:
python lecture_09_unsupervised.py --output-dir pca-plotsThe optional eigendecomposition supplement checks PCA’s directions and eigenvalues directly with NumPy. It uses all 1,797
digits for a descriptive algebra check, so its largest eigenvalue (7.3448)
differs from the worked example’s training-only value (7.3493). It uses the
same requirements:
python lecture_09_pca_supplement.py