Features, Representation & Unsupervised Learning
Preprocessing and representation alter the geometry seen by every estimator. Unsupervised output becomes useful evidence only when its stability and downstream value are tested.
Section 4 of 5 · 8 units · about 29 hours
By the end you can Produce a reproducible feature, PCA and clustering result with invariant-aware tests, then state what the clusters or anomaly scores do and do not justify from stability and downstream evidence.
Units
- Numeric transforms and categorical encoding4h
- Interactions, feature selection and fit-state serialization4h
- PCA via SVD: directions, explained variance and sign ambiguity5h
- k-means: objective, initialization, empty clusters and instability4h
- Gaussian mixtures and EM3h
- DBSCAN, agglomerative clustering and cluster validation4h
- Outlier and novelty detection, thresholds and representation3h
- t-SNE and UMAP as guarded exploratory views2h
Build it
Mini project · about 10 to 14 hours
Feature pipeline, PCA and clustering with invariant-aware tests
Build a train-only feature pipeline, implement PCA and k-means in NumPy with tests that respect sign and label-permutation invariance, then decide from stability and downstream evidence what the components, clusters or anomaly scores do and do not justify.
- 1Build a
ColumnTransformerpipeline with imputation plus missingness indicators, scaling and one-hot encoding withhandle_unknown, fitted on train only. Add a schema test that fails on unexpected columns, and count unknown categories per batch. - 2Implement PCA through the SVD of the centered data: components, explained variance ratio, projection and reconstruction. Compare with scikit-learn PCA through sign-invariant quantities.
- 3Implement k-means with
k-means++initialization. Test that inertia never rises across iterations, and compare withKMeansfrom the same initial centers through the adjusted Rand index, which ignores label order. - 4Add one more method with its own failure mode: a
DBSCANepssweep on moons, or a Gaussian mixture with its covariance type andreg_covarstated. - 5Measure stability: rerun clustering over seeds and bootstrap resamples and report the adjusted Rand distribution. Choose from stability and silhouette together, noting silhouette's bias toward convex clusters.
- 6Test downstream value: feed the components or cluster ids to a supervised model on a fixed split and report the paired change.
- 7Fit an anomaly detector on clean data (
IsolationForest, or LOF withnovelty=True), set the threshold from a stated flag-rate budget, and report the flag rate on a shifted batch with counts and slices.
What you hand in
- The pipeline package with its fit-state and schema tests.
- PCA and k-means implementations with sign- and permutation-invariant tests.
- The evidence report: stability distributions, the choice of k, the downstream effect, the anomaly threshold and a closing paragraph on what the results do and do not justify.
How to check it
- Deep-ML 17, 19, 341, 259 and 367
Public tests. Pass the ones matching your mechanisms after your local tests do; note the sign rule in 19 and the path-length convention the 367 tests require.
- Self-grade with the rubric below
How it's graded
Fit state
Pass: All learned state comes from train only, and a schema test catches unexpected columns.
Great: Unknown categories are counted per batch with an alert rule, and indicator columns survive get_feature_names_out.
PCA
Pass: Components and explained variance match scikit-learn up to sign.
Great: Tests assert only sign-invariant quantities, and they pass across solvers.
Clustering
Pass: k-means matches scikit-learn from the same initial centers (adjusted Rand index 1 on separated blobs), and inertia never rises.
Great: Tests are label-permutation invariant, and the second method's failure mode is shown and explained.
Stability and claims
Pass: Stability over seeds and resamples is reported with the chosen .
Great: A downstream test shows whether the clusters or components help, and the report states what they do not justify.
Anomaly threshold
Pass: The threshold follows from a stated flag budget on clean data.
Great: The flag-rate change on the shifted batch is reported with counts and slices before any threshold change.
Resources
- Hands-On Machine Learning with Scikit-Learn and PyTorch - dimensionality reduction and unsupervised learningAurelien Geron (O'Reilly, 2025)
Read Chapters 7 and 8 and run notebooks 07 and 08 for PCA, k-means, DBSCAN, agglomerative clustering, Gaussian mixtures and anomaly detection. Stop before extended semi-supervised and face-data projects.
- ISLP - Unsupervised LearningJames, Witten, Hastie, Tibshirani and Taylor
Use Chapter 12 and its Python lab for the PCA derivations (12.2, including the low-rank approximation view and proportion of variance explained) and k-means (12.4.1), plus stability and interpretation limits, beside the notebook route.
- Deep-ML - unsupervised problemsDeep-ML
Use 17 K-Means Clustering, 19 PCA, 341 Gaussian Mixture Model with EM, 259 DBSCAN and 367 Isolation Forest as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including the 367 path-length convention.
- scikit-learn - decomposition, clustering and outlier detectionscikit-learn
Check decomposition orientation and solver behavior, cluster API semantics, and the distinction between novelty and outlier detection after the independent tests pass.
- scikit-learn - preprocessing datascikit-learn
Read 8.3.1 standardization with the sparse
with_mean=Falserule, 8.3.1.3 scaling data with outliers, and 8.3.4 categorical encoding withhandle_unknown, infrequent andsparse_outputbehavior. Apply after the HOML notebook build works. - scikit-learn - pipelines and composite estimatorsscikit-learn
Read 8.1.1
Pipelinechaining with the no-leakage rule and transformer caching, and 8.1.4ColumnTransformerfor heterogeneous columns withremainderand fitted-state behavior. Use it to assemble and serialize the feature-selection pipeline. - scikit-learn - imputation of missing valuesscikit-learn
Read 8.4.2 univariate median and
most_frequentstrategies, 8.4.6 Marking imputed values withMissingIndicatororadd_indicator=Truefor missingness flags, and 8.4.7 estimators that acceptNaNdirectly. Decide impute-only versus impute-plus-indicator per column. - UMAP - basic usage and parametersMcInnes, Healy and the UMAP contributors
Work the Penguin scaling and
fit_transformsection and the Digitsfitandtransformsection, then read the parameters page for then_neighborslocal-global tradeoff andmin_distpacking. Stay in 2D exploratory views with fixed seeds.