Foundations to Frontier
Tests
D. Classical Machine Learning › D2 Linear Classifiers

Ridge Classifier

The ridge classifier turns classification into regression: code each class as +1 and the others as −1, fit ridge regression to these codes in one closed-form solve, and predict the class with the largest output. It is extremely fast, handles many classes at once, and for two classes its least-squares direction coincides with Fisher's linear discriminant. Its weakness is the squared loss, which penalises points for being 'too correct' and lets far, easy points pull the boundary. This page covers the encoding, the closed form, the LDA equivalence, the effect of λ, the masking problem with many classes, and when the speed is worth it. In the exercises you implement the ridge classifier and verify the LDA equivalence; then you classify stellar spectral types and compare it with softmax regression on accuracy and training time, with and without far easy points.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Classification by regression

Call one class +1 and the other −1, then fit an ordinary (ridge) regression to those numbers. A new point gets a fitted value; if it is positive, call it +1. For several classes, fit one regression per class (all in one go) and pick the class with the largest output. That is the ridge classifier: no iterations, no probabilities, just one matrix solve.

Ridge classifier: least squares on ±1 labels
training accuracy: ridge classifier 83% · logistic regression 83%
Orange solid: the sign of a least-squares fit to ±1 codes; green dashed: logistic regression. With the extra points near the others, the two agree (for Gaussian classes with equal spread, least squares gives Fisher's LDA direction). As the extra points move far left, squared loss wants their outputs back at −1 instead of −5, so it drags the boundary toward them and misclassifies points in the overlap; the log loss of a confidently correct point is already near zero, so logistic regression stays put.
Why engineers care

When you need a decent linear classifier over many classes and many features right now (for a quick baseline, on-board retraining, or millions of examples), the ridge classifier’s single linear solve is hard to beat. It also connects regression, LDA and classification in one formula.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The ridge classifier fits a regression to…
  2. 2. Training needs…
  3. 3. In the demo, far, correctly classified points…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Fitting codes by hand

Data: x = −2, −1, 1, 2, coded −1, −1, +1, +1.

  • The least-squares line through (x, t) has slope Σxt/Σx² = 6/10 = 0.6 and intercept 0.
  • The boundary is at x = 0, and all four points are correct.

Now add an easy blue point at x = −10, coded −1.

  • The new fit is t = 0.111 + 0.156x, so the boundary moves to x = −0.714.
  • The far point’s output is −1.44: “too negative”. Squared loss charges (−1.44 + 1)² = 0.19 for being more correct than required.
  • The boundary has shifted toward the blue class. Every point is still correct here, but in overlapping data this shift costs accuracy (exercise 2).

The steps in plain English:

  1. One-hot encode the classes as ±1 vectors (+1 in the true class’s column).
  2. Centre X and the codes, then solve (XᵀX + λI)W = XᵀT: one solve for all K columns.
  3. Intercepts restore the means.
  4. Predict the argmax of XW + b.
  5. Tune λ by cross-validation, just as for ridge regression.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The ±1 code for class 2 of 3 is…
  2. 2. Slope of the least-squares fit to x = −2, −1, 1, 2 with codes −1, −1, 1, 1?
  3. 3. Squared loss of output −1.44 against code −1?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

The estimator

Targets: an n × K matrix T with T_ik = +1 if yᵢ = k and −1 otherwise.

Solution: W = (X̃ᵀX̃ + λI)⁻¹X̃ᵀT̃ on centred data, with b = t̄ − x̄ᵀW. The cost is O(np² + p³) once for all classes, or O(n²p) via the dual or an SVD when p > n.

Prediction: ŷ = argmaxₖ (xᵀwₖ + bₖ). For two classes, this is the sign of xᵀ(w₁ − w₀) + (b₁ − b₀).

Relation to LDA. For two classes (λ = 0), the least-squares direction is proportional to Fisher’s direction S_W⁻¹(μ₁ − μ₀) (exercise 1: cosine 1.000000). The intercept differs from LDA’s unless the classes are balanced. With λ > 0 it becomes regularised LDA, with S_W + λI.

No probabilities. The outputs are not in [0, 1] and are not calibrated. Use Platt or isotonic calibration if you need probabilities.

from sklearn.linear_model import RidgeClassifier, RidgeClassifierCV
clf = RidgeClassifierCV(alphas=np.logspace(-3, 3, 13)).fit(X, y)      # efficient leave-one-out over λ
clf.decision_function(X_new)                                          # scores, not probabilities

Use it when you need a fast, stable linear baseline for many classes or for very large data, with standardised features. Avoid it when classes have very different spreads, there are many far easy points, or you need calibrated probabilities.

🐍 Exercise 1: ridge classifier and Fisher's discriminant
Implement ±1 encoding, the ridge classifier in closed form with intercepts, prediction, and Fisher's LDA direction; verify that for two classes the least-squares direction is parallel to Fisher's, that a four-class problem is classified well, and that a huge λ shrinks all weights to zero.
📝 Quick check (Level 2)
0/3 answered
  1. 1. For two classes, the ridge-classifier direction (λ = 0) is parallel to…
  2. 2. RidgeClassifier's decision_function returns…
  3. 3. The K class columns are fitted with…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Why squared loss is a compromise

Loss as a function of the margin m = t·f(x):

  • squared loss: (1 − m)²;
  • logistic loss: log(1 + e⁻ᵐ);
  • hinge loss: max(0, 1 − m).

All three penalise m below 1. Only squared loss also penalises m > 1, that is, points that are confidently correct. Far easy points (large m) therefore pull the fit, as in the demo and in exercise 2.

Masking with many classes (Hastie, Tibshirani and Friedman, §4.2). When K ≥ 3 class means lie roughly on a line, the middle class’s linear output is never the largest anywhere, so it is never predicted. Squared-loss linear regression on indicators “masks” it. Softmax regression and LDA do not suffer from this.

Bayes consistency. With enough flexibility, the squared-loss minimiser of the ±1 code is E[t | x] = 2P(y = 1 | x) − 1. So the sign of the regression is Bayes-optimal if the function class can represent it. With linear functions and misspecified models, the approximation is guided by the wrong loss.

Why use it anyway?

  • It is a closed form, cheap to cross-validate (leave-one-out comes almost free for ridge);
  • it is stable with p ≫ n;
  • it often matches logistic regression on well-behaved data (exercise 2: 0.950 against 0.944).

The least-squares SVM (Suykens, 1999) is the kernelised version.

Interview questions.

  • Why might a ridge classifier be under-predicting one class? Masking, or class imbalance shifting the intercept. Check per-class recall and compare with softmax.
  • How do you get probabilities? Calibrate the decision scores (Platt or isotonic) on held-out data.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Which loss penalises confidently correct points?
  2. 2. Masking means…
  3. 3. The squared-loss minimiser of a ±1 code is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Stellar spectral types in one solve

History.

  • Fisher (1936) introduced the linear discriminant, which equals least squares on class codes for two classes.
  • Hoerl and Kennard (1970) introduced ridge regression.
  • Linear regression on indicator matrices was analysed by Hastie, Tibshirani and Buja (1994).
  • Suykens and Vandewalle (1999) proposed least-squares SVMs.
  • scikit-learn’s RidgeClassifier (with efficient leave-one-out CV) made it a standard fast baseline, especially for text.

Exercise 1. With two classes, the least-squares and Fisher LDA directions have cosine 1.000000, and training accuracy is 0.758. With four classes, accuracy is 0.803. With λ = 10⁹, all weights are below 10⁻⁵.

Exercise 2: stellar spectral types. Setup: 7 classes (O B A F G K M), 60 spectral bins, 1400 training stars, 4 repetitions. The table shows test accuracy.

Scenario Ridge classifier Softmax regression Training time, softmax / ridge
Gaussian classes 0.950 0.944 about 150–180×
15% of class O stars far on the “easy” side 0.854 0.939 about 190–240×

The time ratio varies with the machine: these figures are from a desktop Python run, and in the browser the ratio is about 50–60×, because the browser’s linear-algebra routines slow the closed-form solve down proportionally more than the 400 gradient steps. Either way, the closed form is far cheaper.

  • On well-behaved data the closed form matches softmax regression (slightly better here, since 400 gradient steps are not fully converged) at a tiny fraction of the cost.
  • A few far, easy points cost the ridge classifier 9 accuracy points, while softmax is unaffected. These are hot stars that are trivially recognisable, yet squared loss tries to pull their outputs back to +1 and distorts every boundary.
  • Before using a ridge classifier, check the data for such points (or clip and standardise features). Otherwise use a margin or likelihood loss.
🐍 Exercise 2 (research): stellar types, ridge classifier versus softmax
Simulate 7 stellar spectral classes over 60 bins; compare the ridge classifier with softmax regression by test accuracy and training time, for Gaussian classes and when 15% of one class's training stars lie far on the easy side of every boundary.

The ridge classifier in AI, robotics and aerospace

  • Fast baselines: text and log classification with sparse high-dimensional features.
  • On-board retraining: a single linear solve fits within tight compute budgets.
  • Readout layers: reservoir computing and echo-state networks train their output by ridge regression on class codes.
  • Feature probes: linear probes on frozen neural representations are often fitted this way.

References

  1. R. A. Fisher (1936). “The use of multiple measurements in taxonomic problems.” Annals of Eugenics 7(2).
  2. A. E. Hoerl, R. W. Kennard (1970). “Ridge regression: biased estimation for nonorthogonal problems.” Technometrics 12(1).
  3. T. Hastie, R. Tibshirani, A. Buja (1994). “Flexible discriminant analysis by optimal scoring.” JASA 89(428).
  4. J. A. K. Suykens, J. Vandewalle (1999). “Least squares support vector machine classifiers.” Neural Processing Letters 9(3).
  5. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 4.2. Springer.