S1419 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 19 · Classification lab

Fisher's discriminant from data with a check that no direction does better, k-nearest-neighbor classification with k chosen by leave-one-out, ROC curves and the AUC as a rank statistic, and a Gaussian naive Bayes classifier's log-odds

Lessons 15 to 18 gave four ways to draw a boundary between classes: a projection chosen by Fisher's criterion, a probabilistic model of each class, a logistic model of the boundary itself, and no model at all, just the nearest stored examples. In this lab you'll write the ones that need no iterative training, measure them the way lesson 20 recommends, and check each result against an independent calculation.

  • The curves describe two equally likely unit-variance Gaussian classes whose means are d apart.
  • The solid curve is the Bayes error, the best any classifier can do; the dashed one is the most a 1-nearest-neighbor classifier can reach with unlimited data, 2R*(1−R*).
  • The third is the AUC of the optimal score: the probability that a random positive outscores a random negative, Φ(d/2).

Predict

Ten labeled points on a line include two whose labels are wrong. As k grows from 1 to 5, what happens to a k-NN classifier's leave-one-out error?

Name it

What the lab computes

  • Fisher's discriminant: from the class means 𝐦1, 𝐦2 and the within-class scatter SW=∑k∑n∈Ck(𝐱n−𝐦k)(𝐱n−𝐦k)T, the direction is 𝐰∝SW−1(𝐦2−𝐦1) and its separation is J=(𝐦2−𝐦1)TSW−1(𝐦2−𝐦1), the same as (m2′−m1′)2/(s12+s22) computed from the projections, with sk2 the sum of squared deviations.
  • k-nearest neighbors: a majority vote of the k closest stored examples (ties in distance broken by the earlier example); the leave-one-out error classifies each example by the others.
  • ROC and AUC: sweeping the threshold through every distinct score traces (false-positive rate, true-positive rate) points; the trapezoid area under them equals the Mann–Whitney statistic, the fraction of positive–negative pairs ranked correctly, with ties counting half.
  • Gaussian naive Bayes: each class's prior, per-feature means and maximum-likelihood variances; the log-odds of class 1 at x is the difference of the two classes' log priors plus summed log densities.

Check

Write fisher(X1, X2) for two-dimensional data: return the unit direction (any sign) and the separation J.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write knn_predict(train, labels, x, k) for 0/1 labels and odd k, and loo_error(train, labels, k).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write auc(pos, neg) as the Mann–Whitney statistic, roc(pos, neg) returning the (FPR, TPR) points from (0, 0) through each distinct threshold, highest first, and trapezoid(points).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write fit_gnb(X, y) returning {class: (prior, means, variances)} for classes 0 and 1, and log_odds(x, model).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does the area under the ROC curve equal the probability that a random positive example outscores a random negative one?