Linear Classification
Decision rules, 0-1 loss, gradient descent, logistic regression, and every classification metric.
Half of short-answer section A lives here. Every classification metric, plus the sigmoid, plus gradient descent.
Classification vs regression
- Classification — predict a discrete-valued target. Classic binary case $y\in\{-1,+1\}$.
- Regression — predict a continuous-valued target.
The whole deck is the binary case.
Geometry of a linear classifier
1-D input → you need a threshold. 2-D input → you need a decision boundary that creates two half-spaces.


The model:
$$f(x,w) = w_0 + w^T x = w_0 x_0 + w^T x \quad \text{where } x_0 = 1$$
$w$ is the weight vector, $w_0$ the bias. The prediction rule:
$$y = 1 \text{ if } f(x,w) \ge 0, \qquad y = -1 \text{ otherwise}$$
which is compactly
$$y(x) = \operatorname{sign}\!\big(f(x,w)\big)$$

$w$ controls the direction of the boundary and $w_0$ its location. That split is how the SVM margin derivation later separates "maximise margin" (a statement about $\|w\|$) from "shift the boundary" (a statement about $w_0$).
0-1 loss and why it is awkward
$$L(\hat y, y) = \begin{cases} 1 & \hat y \neq y \\ 0 & \hat y = y\end{cases}$$
It is exactly the quantity you care about, but it is piecewise constant, so its gradient is zero almost everywhere and gradient descent cannot move on it. That is the motivation for a smooth surrogate — the sigmoid for logistic regression, the hinge for SVM.
The deck's discussion prompt is worth thinking through: for diabetes screening, is it worse to call a negative patient positive, or a positive patient negative? The answer determines whether you tune for precision or recall, and it is the standard "why accuracy is not enough" argument.
Gradient descent
- Initialise $w$ (zero or random)
- Repeatedly update $w$ in the direction that reduces the loss:
$$w \leftarrow w - \alpha \nabla_w L(w)$$ where $\alpha$ is the learning rate

Three consequences to keep straight: too large $\alpha$ diverges, too small converges slowly, and the gradient is only defined for differentiable losses.
Logistic regression
Replace the hard sign with the smooth sigmoid:
$$y(x) = \sigma(w^T x + w_0), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}$$

The output is now $p(y\mid x)$, a probability in $(0,1)$; you threshold it to get a class. That is the difference between a discriminant and a probabilistic classifier, and it is why logistic regression can produce an ROC curve at all.
Why classes are not always separable

- the same feature values occur with different labels
- the model is too simple
- noisy inputs
- features are too simple (diabetes from age and gender only)
- mislabelled data
And the remedies the slide gives: add more features, build a more complex classifier, or accept some errors. That last one is the soft-margin idea in one phrase.
The metric table — memorise this
Given the confusion matrix with TP, FP, FN, TN:
| Metric | Formula | Reads as |
|---|---|---|
| Accuracy | $\dfrac{TP+TN}{TP+TN+FP+FN}$ | fraction correct |
| Precision | $\dfrac{TP}{TP+FP}$ | of predicted positives, how many are right |
| Recall / Sensitivity / TPR | $\dfrac{TP}{TP+FN}$ | of actual positives, how many found |
| Specificity / TNR | $\dfrac{TN}{TN+FP}$ | of actual negatives, how many rejected |
| F1 | $\dfrac{2\cdot P\cdot R}{P+R} = \dfrac{2TP}{2TP+FP+FN}$ | harmonic mean of P and R |
| FPR | $\dfrac{FP}{FP+TN} = 1-\text{specificity}$ | x-axis of ROC |


The np.diag trap. The lecture notebook computes FP = conf.sum(axis=0) - np.diag(conf); TP = np.diag(conf). That idiom silently assumes class 0 is the positive class. If your label of interest is class 1 — which it is on the diabetes dataset — np.diag returns [TN, TP] and your F1 array is [F1_class0, F1_class1] unlabelled. This exact confusion cost marks in Assignment 1. Always unpack explicitly: TN, FP, FN, TP = confusion_matrix(y, p, labels=[0,1]).ravel()
ROC and AUC

- ROC plots TPR (sensitivity) against FPR (1 − specificity) as the threshold sweeps.
- The diagonal is random guessing.
- AUC is the aggregate: probability that the classifier ranks a random positive above a random negative. 0.5 = chance, 1.0 = perfect.
ROC needs a continuous score (predict_proba, decision_function). If you feed it hard 0/1 labels, roc_curve collapses to at most three points and the "AUC" you get equals balanced accuracy at that single operating point. It is not wrong to plot it, but you must say what it is.



































