Part IX — Dimensionality Reduction

Chapter 30 · Principal Component Analysis

Part IX asks the opposite of Part VIII's question: not what more a network can see, but which directions of a dataset matter. The Wine dataset measures each of 178 wines on 13 chemical features — yet the three grape varieties separate almost perfectly on just two axes nobody measured. Principal Component Analysis finds those axes: the directions where the data varies most. The method is built by hand on four points — covariance matrix, characteristic polynomial, eigenvectors, projections, and the exact arithmetic of what compression costs — then run for real on all 13 features, where every eigenvalue, loading, and classification score on this page comes from the actual run.

📖 Chapter 30 of 35 ⏱ ~95 min read Mostly Core Stable Volatile 📝 Quiz · 30 MCQs

🎯 Learning Objectives

1. Thirteen Features, Three Wines

The running dataset: Wine.csv — 178 wines from three Italian cultivars (59 + 71 + 48), each measured on 13 chemical features: alcohol, malic acid, ash, alkalinity of ash, magnesium, total phenols, flavanoids, non-flavanoid phenols, proanthocyanins, colour intensity, hue, OD280, and proline. The task behind the task: these 13 numbers describe wines from three grapes — how much of that 13-dimensional measurement is actual information?

Chapter 12 named the disease: the curse of dimensionality — more columns mean sparser data, farther neighbours, noisier models. But wine chemistry has a deeper property, one Chapter 5 diagnosed as multicollinearity: the features correlate. Flavanoids moves with total phenols (r = +0.86 — phenols are partly flavanoids); colour intensity moves against hue (r = −0.52); proline moves with alcohol (r = +0.64). Thirteen columns carry far fewer than thirteen independent stories. Dimensionality reduction is the family of methods that finds the true stories and keeps only them. PCA is the founding member.

What reduction buysWhere you will see it today
Visualisation — 13-D cannot be plotted; 2-D canSection 6: three grape varieties, visible by eye
Compression — fewer numbers to store, faster models2 numbers per wine instead of 13
De-noising — small-variance directions are often noiseSection 6: last 6 PCs carry ~10% of variance
Decorrelation — PCs are uncorrelated by constructionSection 3: the eigenvectors are orthogonal
Collinearity cure — regression on PCs has no VIF problemChapter 5's 50_Startups diagnosis, resolved in principle
📝
Note: throughout Part IX the pipeline splits the data first (142 train / 36 test here, seed 0) and standardises on the training set only — Chapter 3's leakage rule. Why scaling is not optional here becomes the chapter's loudest warning.

2. The Idea: Variance is Information

Picture the wines as a cloud of 178 points in 13-dimensional space. Now photograph that cloud from the one direction that shows it most spread out — that photograph is the first principal component. Photograph it again from the perpendicular direction with the next-most spread: the second component. Repeat, and you have a new coordinate system, ordered by how much of the data's scatter each axis captures.

The working hypothesis is audacious and usually right: the directions along which data varies most are where the information lives. Directions where every wine measures nearly the same tell you nothing that distinguishes them. PCA formalises "spread out" as variance, and hunts for the axes of maximal variance.

Why not simply delete the least-useful column? Because correlated columns say the same thing — delete one and its twin keeps re-telling the story. PCA instead merges them: it builds new axes as weighted combinations of all features — every PC is a recipe using all 13 ingredients in optimized proportions — and keeps only the recipes that matter. Chapter 13's SVM already weighs every feature in one combination; PCA does the same, but chooses the combinations without looking at y — and variance is only a proxy for usefulness (Section 8 shows where that proxy fails).

💡
Pro Tip — two equivalent definitions: the first PC is simultaneously (a) the projection direction with maximum variance of the projected data, and (b) the line with minimum squared reconstruction error when the data is compressed onto it. Section 3 verifies both numerically — they are the same theorem seen from two sides.

3. PCA by Hand — Four Points, Two Axes

Four wines, two features (say, acidity and sweetness — units don't matter to the arithmetic): (3, 3), (7, 7), (6, 4), (4, 6). Every step below is exact, and verified against the library at the end.

3.1 Center

PCA measures deviations, so first subtract the mean (5, 5). The centered data: (−2, −2), (2, 2), (1, −1), (−1, 1). The cloud now tilts along the diagonal — the eye already sees the answer; PCA will compute it.

3.2 The covariance matrix

C = (1/n) · ZTZ  = 
2.51.5
1.52.5
var(x) = (4+4+1+1)/4 = 2.5 · var(y) = 2.5 the same way · cov = (−2·−2 + 2·2 + 1·−1 + −1·1)/4 = (4+4−1−1)/4 = 1.5. The off-diagonal 1.5 > 0 is the tilt: x and y move together.

3.3 Eigendecomposition — the axes fall out

Every symmetric matrix has perpendicular eigenvectors, and PCA's whole claim is that the eigenvectors of the covariance matrix are the principal axes. Solve by hand with the characteristic polynomial:

det(C − λI) = (2.5 − λ)² − 1.5² = λ² − 5λ + 4 = (λ − 4)(λ − 1) = 0
λ₁ = 4 and λ₂ = 1. Eigenvectors: for λ₁ = 4, (C − 4I)v = 0 gives −1.5v₁ + 1.5v₂ = 0 → v₁ = v₂ → v₁ = (1, 1)/√2 ≈ (0.707, 0.707) — the 45° diagonal. For λ₂ = 1: 1.5v₁ + 1.5v₂ = 0 → v₂ = (1, −1)/√2 — perpendicular, as promised.

3.4 Project: the scores

Each point's new coordinates are dot products with the eigenvectors. The score on PC1 = (x + y)/√2 after centering:

Point (centered)PC1 score = (x+y)/√2PC2 score = (x−y)/√2
(−2, −2)−4/√2 = −2.830
(2, 2)+4/√2 = +2.830
(1, −1)0+2/√2 = +1.41
(−1, 1)0−2/√2 = −1.41
PC1 (λ=4)PC2 (λ=1) (3,3) (7,7) (6,4) (4,6) mean (5,5) (6,4) projects to 0 on PC1 axes drawn at equal scale, so PC1 ⟂ PC2 looks perpendicular
Figure 30.1 — The hand example, drawn at equal axis scale. PC1 (solid) runs along the diagonal — maximum spread; PC2 (dashed) is perpendicular, and this time it looks it. Point (6, 4) has PC1 score 0: it sits exactly at the mean along the main axis.

3.5 The variance ledger — and what compression costs

The eigenvalues are the variances of the scores. Check: mean of PC1² = (8 + 8 + 0 + 0)/4 = 4 = λ₁; mean of PC2² = (0 + 0 + 2 + 2)/4 = 1 = λ₂. Two consequences, both exact:

explained variance PC1 = λ₁ / (λ₁ + λ₂) = 4/5 = 80%  ·  PC2 = 20%

compression to PC1: average squared reconstruction error = λ₂ = 1
Verify the error claim on (6, 4): its PC1 score is 0, so reconstruction = mean + 0 = (5, 5); the error vector is (1, −1), squared length 2. Across all four points the squared errors are 0, 0, 2, 2 — average 1, exactly the discarded eigenvalue. The variance you throw away is the error you eat, coin for coin. (Library check: sklearn on the same four points returns EVR 80/20 and identical reconstructions; its explained_variance_ prints 5.33 and 1.33 because sklearn divides by n − 1 = 3 — the eigenvalues scale, the ratios do not.)
⚠
Warning — sign is a costume: an eigenvector and its negative are the same axis; sklearn may return (−0.707, −0.707) where the hand computation gives (0.707, 0.707), flipping score signs. Nothing has changed — the axis, variance, and reconstructions are identical. Sign ambiguity is a classic PCA gotcha when comparing implementations.

4. The Algorithm, and What the Eigenvalues Mean

The complete recipe, exactly as executed by hand above:

  1. Center each feature (subtract its mean) — PCA is about deviations.
  2. Scale each feature to unit variance — otherwise big-unit features hijack the axes (Section 6 shows how badly).
  3. Covariance matrix C — p × p for p features; captures every feature's spread and every pair's co-movement.
  4. Eigendecompose C = VΛVT: eigenvalues λᵢ (variances along each axis), eigenvectors (the axes, mutually perpendicular).
  5. Sort by λ descending — PC1, PC2, …
  6. Project: scores T = ZV; keep the first k columns to compress.

Three structural facts make the method feel engineered rather than lucky. Conservation: the eigenvalues sum to the trace of C — total variance is preserved, merely repackaged by axis. On the scaled Wine data, 13 features each carry variance 1, total 13 — and the real eigenvalues of Section 6 sum to 13.09, because scikit-learn's explained_variance_ divides by n − 1 = 141 instead of n = 142: 13 × 142/141 = 13.09. The percentages are unaffected; a ratio does not care. Orthogonality: the PCs are uncorrelated by construction — the collinearity disease of Section 1 is cured outright, each PC a genuinely independent story. Best rank-k: keeping the top k axes is the rank-k compression with minimum squared error — the Eckart–Young theorem, not a heuristic.

📝
Note — SVD in practice: software does not form the 13 × 13 covariance matrix and factorise it; it applies the singular value decomposition directly to the centered data matrix — numerically stabler, same result (singular values are the square roots of the eigenvalues, up to a factor of √(n−1)). The hand route and the library route meet at the same axes.
📋 Stable content — Reviewed: October 2026

5. Implementation in Python

import numpy as np
import pandas as pd

dataset = pd.read_csv('Wine.csv')
X = dataset.iloc[:, :-1].values      # 13 chemical features
y = dataset.iloc[:, -1].values       # Customer_Segment: 1, 2, 3

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=0)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)  # scale on train ONLY
X_test = sc.transform(X_test)

from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_train = pca.fit_transform(X_train) # fit PCA on train ONLY
X_test = pca.transform(X_test)
print(pca.explained_variance_ratio_)  # [0.369, 0.193]

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(X_train, y_train)

from sklearn.metrics import confusion_matrix, accuracy_score
y_pred = classifier.predict(X_test)
print(confusion_matrix(y_test, y_pred))   # [[14  0  0]
                                          #  [ 1 15  0]
                                          #  [ 0  0  6]]
print(accuracy_score(y_test, y_pred))     # 0.972

Listing 30.1 — The complete pipeline, adapted from the course file principal_component_analysis.py (Part 9 — Dimensionality Reduction). Scaling and PCA both fit on the training set only — the test set is transformed with frozen parameters. The script overwrites X_train/X_test as it goes; the Hands-On code keeps separate names.

# The idioms worth knowing (X, y are still the raw arrays above;
# re-split with the same seed so the rows match Listing 30.1)
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score, StratifiedKFold

Xr_tr, Xr_te, yr_tr, yr_te = train_test_split(X, y, test_size=0.2,
                                              random_state=0)
pipe = Pipeline([('scale', StandardScaler()),
                 ('pca', PCA(n_components=2)),
                 ('clf', LogisticRegression(random_state=0))])
pipe.fit(Xr_tr, yr_tr)                   # leakage-proof order, guaranteed
print(pipe.score(Xr_te, yr_te))          # 0.972 — Listing 30.1 in one line

PCA(n_components=0.95)   # keep 95% of variance — k chosen automatically

# honest model comparison: CV the whole pipeline, all 178 wines
cv = StratifiedKFold(10, shuffle=True, random_state=0)
for k in (1, 2, 3, 5, 7, 13):
    pipe_k = Pipeline([('scale', StandardScaler()), ('pca', PCA(k)),
                       ('clf', LogisticRegression(max_iter=1000))])
    s = cross_val_score(pipe_k, X, y, cv=cv)
    print(k, round(s.mean(), 3), round(s.std(), 3))   # Section 6.2's CV table

# reproduce the scree plot of Figure 30.2 (bars + Kaiser line; the
# cumulative curve needs a twin y-axis — plt.twinx())
import matplotlib.pyplot as plt
full = PCA().fit(StandardScaler().fit_transform(Xr_tr))
plt.bar(range(1, 14), full.explained_variance_)
plt.axhline(1, ls='--', c='gray')
plt.show()

Listing 30.2 — The Pipeline idiom (leakage-proof by construction), variance-targeted component choice, the cross-validated k-sweep of Section 6.2, and the scree reproduction.

💡
Pro Tip: PCA(n_components=0.95) is the professional default when you don't want to argue about k: hand it a variance target instead of a count. On this data it keeps 10 components (Section 7's table shows why).

6. The Real Run — Wine in Two Dimensions

The real decomposition of the 13 scaled training features — every eigenvalue below from the actual fit:

PC1PC2PC3PC4PC5PC6PC7PC8PC9PC10PC11PC12PC13
λ (eigenvalue)4.832.531.410.970.820.640.540.330.300.240.230.160.09
Variance %36.919.310.87.46.34.94.12.52.31.91.71.30.7
Cumulative %36.956.267.074.480.685.589.692.294.596.398.199.3100

The ledger balances: the eigenvalues sum to 13.09 — total variance conserved, with the n − 1 footnote of Section 4. And the shape of the table is the classic PCA signature: two strong components (36.9% + 19.3% = 56.2%), a shoulder at PC3, then a long shallow tail — the last six axes together carry about 10% of the variance. Two strong directions, a third clearly above the noise (Kaiser's rule, next section), and a tail that still holds about a third of the variance: the redundancy Section 1 suspected, now quantified — without collapsing to any single magic number.

scree plot: eigenvalues, Kaiser line, cumulative variance 543 210 100%50%0% Kaiser: λ = 1 123 456 789 10111213 principal component eigenvalue λ (left axis) cumulative variance (right axis)
Figure 30.2 — The scree plot of the real run. Eigenvalue bars fall off a cliff after PC1–PC2 and flatten into a shallow tail; the dashed line is Kaiser's λ = 1 cut (three bars clear it); the green line is cumulative variance on the right axis. The elbow sits at PC2–PC3.

One more ledger entry the hand example promised: compression costs the discarded eigenvalues. At k = 2 the dropped eigenvalues sum to 13.09 − 7.36 = 5.73 — 44% of the variance — and the measured mean squared reconstruction error per wine is 5.69 standardised units, about 0.44 per feature (the small gap from 5.73 is the n vs n − 1 convention again). pca.inverse_transform reconstructs any wine from its two scores; the Hands-On lets you watch the bill being paid.

6.1 The two-dimensional photograph

Project all 142 training wines onto the first two PCs — the classic picture of this dataset, drawn here from the real coordinates (we swap the usual decision-region contours for the raw scatter; the clusters do the talking):

-4 -2 2 4 -3 -2 -1 1 2 3 PC1 (36.9%) →↑ PC2 (19.3%) cultivar 1 cultivar 2 cultivar 3 × = class mean
Figure 30.3 — All 142 training wines, real PC coordinates, equal axis scale. Cultivar 1 (circles) sits high-right, cultivar 2 (triangles) low-centre, cultivar 3 (squares) left — different shapes as well as colours, for colour-blind reading. × marks each class mean: (2.39, 1.00), (0.09, −1.68), (−2.67, 1.13). Three clusters — and PCA never looked at the labels.

The punchline of the whole part: PCA is unsupervised — the class labels were never shown to it — yet the three cultivars separate into three visible islands. The grape's identity was hiding inside the chemistry's variance structure all along.

6.2 Classify on two numbers

The loop closes with logistic regression (Chapter 11's classifier — scikit-learn handles the three classes by default) on the two PC scores — train, then the 36-wine test set:

Model (one split: 142 train / 36 test, seed 0)Confusion matrixAccuracyFeatures used
Logistic on PC1 + PC2[[14, 0, 0], [1, 15, 0], [0, 0, 6]]0.9722 (56.2% of variance)
Logistic on all 13 scaled features[[14, 0, 0], [0, 16, 0], [0, 0, 6]]1.00013 (100%)
Logistic on PC1 only—0.8061 (36.9%)
Logistic on PCs 1–3—1.0003 (67.0%)
Logistic on PCs 1–5—0.9725 (80.6%)
Logistic on PCs 1–7—1.0007 (89.6%)

Read this table twice, because it teaches two opposite lessons. First: two numbers per wine — 15% of the columns, 56% of the variance — classify 35 of 36 test wines correctly. The compression is real. Second — and subtler: accuracy does not track variance exactly. Three PCs already match the perfect 13-feature score, and five PCs fall back to 0.972. But do not over-read that dip: with 36 test wines, one wine is 2.8 percentage points. k = 2, 4 and 5 each misclassify exactly one wine; k = 3, 6, 7, 10 and 13 misclassify none. The wiggle is one wine, not a law — Chapter 18's small-sample caution, live.

So measure properly: cross-validate the whole pipeline — scale, PCA(k), classifier inside one Pipeline, refit on every fold, all 178 wines (Listing 30.2). The real numbers:

k (components)1235713
10-fold CV accuracy0.8430.9600.9720.9830.9780.983
CV standard deviation0.0600.0450.0280.0250.0270.025

Under cross-validation the story is clean: accuracy rises with k and levels off around 5. Two PCs already capture most of the class structure (0.96); five match all thirteen (0.983); the single split's k = 5 "drop" disappears — the differences between k = 2, 3 and 5 sit within one standard deviation. Variance remains a heuristic for information, not a proof: PCA never sees y, and nothing guarantees the class-bearing direction is a high-variance one. Imagine two long horizontal clouds, one stacked above the other: PC1 follows the length of the clouds — most variance, no class information — while the thin PC2 carries the separation. When labels exist and separation is the goal, Chapter 31's LDA aims the axes at exactly that.

⚠
Warning — the scaling trap: skip the StandardScaler and PCA on raw Wine data would be proline-analysis: proline's variance (std ≈ 304 on the training set) is over a million times hue's (std 0.23), so PC1 would point almost entirely along the proline axis — not because proline is informative, but because it is measured in big numbers. Chapter 12's KNN lesson, now with eigenvectors. Standardise first, always.

7. Choosing k & Reading Loadings

7.1 Three ways to pick the number of components

CriterionRuleOn this data
Scree elbowplot λ vs PC (Figure 30.2); keep components before the curve flattenselbow at PC2–PC3 → keep 2–3
Cumulative thresholdsmallest k reaching a target variance80% → 5 · 90% → 8 · 95% → 10 · 99% → 12
Kaiser criterionkeep PCs with λ > 1 (more variance than one original feature)λ = 4.83, 2.53, 1.41 → 3 components

All three point the same way here: two or three components. Kaiser's λ > 1 rule picks 3 — which also happened to be the single split's best k — though Section 6.2's cross-validation says everything from k = 3 up ties within noise. The scaled data's "one original feature" benchmark of variance 1 is what makes the rule's logic visible (a PC must out-earn a single feature to be worth keeping).

7.2 Loadings — what the axes mean

Each PC is a weighted recipe over the original features; the weights are the loadings — here the rows of pca.components_, unit-length eigenvectors (some texts multiply by √λ and call that the loading; we do not). The real values, strongest six per component:

PC1 loadingsvaluePC2 loadingsvalue
Flavanoids+0.428Colour intensity+0.531
Total phenols+0.389Alcohol+0.498
OD280+0.376Proline+0.365
Proanthocyanins+0.308Ash+0.315
Hue+0.307Hue−0.272
Non-flavanoid phenols−0.305Magnesium+0.258

The chemistry reads off the page. PC1 is a phenolic-content axis: heavy positive weights on flavanoids, total phenols, OD280 (an absorbance ratio of diluted wine) and proanthocyanins, negative on non-flavanoid phenols — phenolic-rich wines score high, phenolic-poor wines low. (PC1 could equally be printed with every sign flipped; "high score = phenolic-rich" is a convention, as the sign warning said.) PC2 is a body/colour axis: colour intensity, alcohol and proline up — big, strong, dark wines high. A wine's two PC scores are, roughly, "phenolic character" and "body" — two summary measurements a wine chemist would endorse, discovered from correlation structure alone.

💡
Pro Tip: loadings are also the debugging tool when PCA misbehaves: if one feature dominates a loading vector almost alone, you forgot to scale (or that feature genuinely is the story — the loadings cannot tell you which; domain knowledge must).
⚠ Volatile content — Reviewed: October 2026 · Next review: January 2027

8. Limitations & the Road Ahead

The honest ledger — assumptions and failure modes first:

Next chapter: Linear Discriminant Analysis — the same eigen-machinery, aimed by the labels. Where PCA asks "along which direction do wines differ most from each other?", LDA asks "along which direction do the three cultivars differ most from each other?" One word of difference in the question; a different matrix in the computation; and, usually, a sharper separator.

🛠 Hands-On: The Sommelier's Two NumbersIntermediate

A wine retailer wants each wine catalogued by two summary scores instead of 13 lab measurements. Build the transformation, verify every hand claim, then advise: are two numbers enough, and what do they mean?

Steps

  1. Hand-verify Section 3 completely: covariance entries, characteristic polynomial (λ−4)(λ−1), eigenvectors, all eight scores, the 80/20 split, and the reconstruction of (6, 4) → (5, 5) with squared error 2.
  2. Confirm the hand result against sklearn on the same four points: PCA(2) EVR must be [0.8, 0.2] and the reconstruction from PC1-only must be [(3,3), (7,7), (5,5), (5,5)]. Explain any sign differences.
  3. Run the Wine pipeline (Listing 30.1); verify the eigenvalue table's first three entries (4.83, 2.53, 1.41) and their sum, 13.09 (sklearn's n − 1 convention — Section 4).
  4. Reproduce the k-sweep (k = 1, 2, 3, 5, 7, 10, 13) and the two headline accuracies: 0.972 on two PCs, 1.000 on thirteen. Then run Listing 30.2's cross-validated sweep and watch the single split's k = 5 dip vanish.
  5. Choose k by all three criteria of Section 7.1 and defend a recommendation to the retailer in three sentences.
  6. Bonus: rerun PCA WITHOUT StandardScaler; report PC1's explained variance and top loading, and explain what the axis now measures.
View Solution / Walkthrough
import numpy as np, pandas as pd
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# ---- steps 1-2: hand example vs library
pts = np.array([[3,3],[7,7],[6,4],[4,6]], float)
p = PCA(n_components=2).fit(pts)
print(p.explained_variance_ratio_)        # [0.8 0.2]
T = p.transform(pts)
T1 = T.copy(); T1[:,1] = 0                # keep PC1 only
print(p.inverse_transform(T1))            # [[3,3],[7,7],[5,5],[5,5]]

# ---- steps 3-5: the Wine pipeline
ds = pd.read_csv('Wine.csv')
X, y = ds.iloc[:, :-1].values, ds.iloc[:, -1].values
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=.2, random_state=0)
sc = StandardScaler(); Xtr_s = sc.fit_transform(Xtr); Xte_s = sc.transform(Xte)
full = PCA().fit(Xtr_s)
print(full.explained_variance_[:3].round(3))   # [4.829 2.529 1.408]
print(full.explained_variance_.sum().round(3)) # 13.092 — 13*142/141 (sklearn: n-1)
for k in [1,2,3,5,7,10,13]:
    pk = PCA(n_components=k).fit(Xtr_s)
    acc = LogisticRegression(random_state=0).fit(
        pk.transform(Xtr_s), ytr).score(pk.transform(Xte_s), yte)
    print('k=%2d  cum var %5.1f%%  acc %.3f'
          % (k, full.explained_variance_ratio_[:k].sum()*100, acc))

# ---- step 6 (bonus): the scaling trap, witnessed
raw = PCA(2).fit(Xtr)                     # NO scaler
print(raw.explained_variance_ratio_)      # PC1 ≈ 0.998
top = np.abs(raw.components_[0]).argmax()
print('PC1 is dominated by:', ds.columns[top])   # Proline

Expected results (real): hand/library agree exactly (EVR 80/20; reconstruction to (5,5) twice; mean squared error 1 = λ₂; sklearn's explained_variance_ prints 5.33 and 1.33 — the n−1 convention). Wine: eigenvalues 4.829, 2.529, 1.408, … summing to 13.09; single-split acc 0.806 (k=1), 0.972 (k=2), 1.000 (k=3), 0.972 (k=5), 1.000 (k=7, 10, 13); cross-validated 0.843 / 0.960 / 0.972 / 0.983 / 0.978 / 0.983 (±0.03–0.06) for k = 1 / 2 / 3 / 5 / 7 / 13. Without scaling, PC1 captures ≈99.8% of variance and points almost entirely along Proline — the milligram scale, not the chemistry. Model answer (Step 5): catalogue every wine by two scores — they are human-readable (phenolic content, body), keep 56.2% of the variance, and classify at 0.972 on the test split and 0.96 under cross-validation. Store the third component but do not display it: Kaiser's rule and the scree elbow both endorse it, and it costs little. Anything past three is a long tail — about 10% of the variance spread over six axes — not worth a column in a shop catalogue.

🎯 Key Takeaways

1

Correlated features overcount the same information: 13 wine measurements are two strong directions, a clear third, and a tail holding a third of the variance. PCA repackages that variance into perpendicular axes ranked by importance.

2

The eigenvectors of the covariance matrix ARE the principal axes — hand-verified: λ² − 5λ + 4 = 0 → λ = 4, 1 on the 45° diagonal, 80%/20% split.

3

Discarded eigenvalues are the compression bill: keeping PC1 only, mean squared error = λ₂ = 1, exactly. Max variance and min error are one theorem.

4

Real Wine run: PC1 36.9% + PC2 19.3% = 56.2%, eigenvalues sum to 13.09 (sklearn's n − 1); three cultivars separate visibly — and PCA never saw the labels.

5

Two numbers classify 35/36 test wines (0.972); cross-validated, k = 2 reaches 0.96 and k = 5 matches all 13. Variance is a heuristic for information, not a guarantee.

6

Scale first — unscaled Wine makes PCA "proline-analysis" (PC1 ≈ 99.8% one feature). Loadings make axes readable: PC1 = phenolic content, PC2 = body/colour.

Test Your Understanding

30 questions across the chapter — the hand example, the eigendecomposition, the real Wine run (single split and cross-validated), choosing k, the scree plot, and honest limitations. Choose one answer per question, then check your answers.

Q1.The dataset and the task:

Q2.Thirteen features often hide far fewer stories because:

Q3.PCA's working hypothesis:

Q4.Why build new axes as combinations instead of deleting weak columns?

Q5.(Hand) From C = [[2.5, 1.5], [1.5, 2.5]]: the eigenvalues and the explained-variance split are:

Q6.(Hand) The first principal component points along:

Q7.(Hand) The PC1 score of centered point (1, −1) is:

Q8.(Hand) Keeping only PC1, the average squared reconstruction error equals:

Q9.(Real) sklearn's explained_variance_ on the four hand points prints 5.33 and 1.33, not 4 and 1, because:

Q10.The PCA algorithm's correct order:

Q11.Why must features be standardised before PCA here?

Q12.PCA is called unsupervised because:

Q13.Variance conservation on the real run says:

Q14.(Real) The first two components capture:

Q15.(Real) Logistic regression on the two PCs scored:

Q16.(Real) The single-split k-sweep's honest reading:

Q17.(Real) Under 10-fold cross-validation (all 178 wines):

Q18.(Real) Components needed by cumulative-variance targets:

Q19.Kaiser's criterion (keep λ > 1) selects:

Q20.A scree plot (Figure 30.2) is read by:

Q21.(Real) PC1's loadings say it is:

Q22.(Real) The three cultivar means in PC space:

Q23.The leakage-safe fit order for the PCA pipeline:

Q24.PCA(n_components=0.95) means:

Q25.Your hand PC1 is (0.707, 0.707); sklearn returns (−0.707, −0.707). This means:

Q26.PCA's linearity assumption fails when:

Q27.The cleanest demonstration that variance ≠ class information:

Q28.The interpretability tax of PCA:

Q29.PCA by itself is:

Q30.Chapter 31 (LDA) differs from PCA by: