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, .
- The third is the AUC of the optimal score: the probability that a random positive outscores a random negative, .
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?
With k = 1, each mislabeled point misleads its neighbors and is itself misclassified, and task 2's data give a leave-one-out error of 60%. With k = 5 the vote outweighs a single wrong label and the error falls to 30%. Larger k trades resolution for robustness to noisy labels, and leave-one-out is a cheap way to choose it.
Name it
What the lab computes
- Fisher's discriminant: from the class means , and the within-class scatter , the direction is and its separation is , the same as computed from the projections, with 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?
Each step along the ROC curve lowers the threshold past one more score. When it passes a negative's score, the false-positive rate rises by one negative's share and the curve moves right at the current true-positive rate, which is the fraction of positives scoring above that negative. The area added is therefore that negative's share times the fraction of positives that outrank it. Summing over all negatives gives the average, over negatives, of the fraction of positives scoring higher, which is exactly the probability that a randomly chosen positive outscores a randomly chosen negative, with ties, where the curve rises diagonally, counting half.