---
title: Linear classification
heading: Linear discriminants and the geometry of their decision boundaries, the signed distance to a hyperplane, Fisher's discriminant that projects onto the direction separating classes best, and the perceptron with its convergence bound
eyebrow: S14 · Lesson 15 · Linear classification
xp: 50
---

::: hook
The simplest classifier draws a straight line (or a flat hyperplane) through feature space and calls one side positive. How to place it is the interesting part. Fisher's 1936 answer was to look for the direction along which the two classes' projections are as far apart as possible relative to their spread, a direction that often isn't the one joining the class means. Rosenblatt's perceptron took the opposite approach, nudging the line every time it made a mistake, and was guaranteed to stop if any line could separate the data.
:::

::: play
- Two classes in two dimensions with means differing by (2, 2) and a shared within-class covariance with variances 1 and 4. Project onto a direction at angle θ and measure the separation $J(\theta) = (\Delta m\text{ along }\theta)^2/(\text{spread along }\theta)$.
- Projecting along the line between the means (45°) gives J = 3.2; Fisher's direction, tilted toward the low-variance axis, gives J = 5.
- Change the variances to see the best direction move.
:::

```sim plot
view: graph
x: t = -90..90 °
y: J = 0..8
slider: a = 0.25..4 step 0.25 = 1
slider: b = 0.25..4 step 0.25 = 4
plot: (2*cos(t*pi/180) + 2*sin(t*pi/180))^2/(a*cos(t*pi/180)^2 + b*sin(t*pi/180)^2) | samples: 600 | label: separation J(θ)
readout: Fisher's angle, ° = atan((2/b)/(2/a))*180/pi | digits: 4
readout: best J = 4/a + 4/b | digits: 4
```

::: predict
## A linear classifier computes $\mathbf{w}^T\mathbf{x} + w_0$ with $\mathbf{w}$ = (3, 4) and $w_0$ = −5. How far is the point (3, 1) from its decision boundary?

- [ ] 8
- [x] 1.6, on the positive side
- [ ] −1
- [ ] 0.6

::: reveal
The boundary is $\mathbf{w}^T\mathbf{x} + w_0 = 0$, and the signed distance of a point is $(\mathbf{w}^T\mathbf{x} + w_0)/\|\mathbf{w}\|$ = (9 + 4 − 5)/5 = 1.6. The origin sits at −5/5 = −1, on the negative side.
:::
:::

::: nameit
## Linear discriminants

A two-class linear discriminant assigns $\mathcal{C}_1$ when $y(\mathbf{x}) = \mathbf{w}^T\mathbf{x} + w_0 > 0$. The boundary is a hyperplane perpendicular to **w**, at distance $-w_0/\|\mathbf{w}\|$ from the origin, and $y(\mathbf{x})/\|\mathbf{w}\|$ is a point's signed distance from it. K classes use K functions and pick the largest.

## Fisher's discriminant

Project onto a direction **w**. With class means $\mathbf{m}_1$, $\mathbf{m}_2$ and within-class scatter $S_W$, the separation

$$J(\mathbf{w}) = \frac{(\mathbf{w}^T(\mathbf{m}_2 - \mathbf{m}_1))^2}{\mathbf{w}^TS_W\mathbf{w}}$$

is largest for $\mathbf{w} \propto S_W^{-1}(\mathbf{m}_2 - \mathbf{m}_1)$, where $J = \Delta\mathbf{m}^TS_W^{-1}\Delta\mathbf{m}$. In one dimension, J is the squared difference of projected means over the sum of projected variances.

## The perceptron

With targets $t_n = \pm 1$, cycle through the data and, whenever $t_n\mathbf{w}^T\mathbf{x}_n \le 0$ (a mistake), update $\mathbf{w} \leftarrow \mathbf{w} + \eta t_n\mathbf{x}_n$. If the data are linearly separable with margin γ by a unit vector, and every $\|\mathbf{x}_n\| \le R$, it converges after at most $(R/\gamma)^2$ mistakes. If they aren't separable, it never settles.
:::

::: example
## Find Fisher's direction and its separation for the sim's classes (Δm = (2, 2), $S_W$ = diag(1, 4)).

::: solution
1. **Fisher's direction.**
   $$S_W^{-1}\Delta\mathbf{m} = (2, 0.5)$$
   at an angle of $\tan^{-1}(0.25)$ = 14.0° from the first axis.
2. **Separation.**
   $$\begin{aligned} J &= 2^2/1 + 2^2/4 \\ &= 5 \end{aligned}$$
3. **Along the means' own direction.** At 45°:
   $$\begin{aligned} J &= \frac{(4/\sqrt{2})^2}{(1 + 4)/2} \\ &= 3.2 \end{aligned}$$
   Ignoring the spread wastes separation.

**Answer:** Fisher's direction is (2, 0.5), 14.0° from the first axis, with a separation J = 5, against 3.2 along the means' own direction.
:::
:::

::: example
## A perceptron with w = (1, −2) and η = 0.5 misclassifies x = (2, 1) with t = +1. What's w after the update? And how many mistakes can it make on data with R = 5 and margin 0.5?

::: solution
1. **Update.**
   $$\begin{aligned} \mathbf w &= (1, -2) \\ &\quad + 0.5 \times (+1) \times (2, 1) \\ &= (2, -1.5) \end{aligned}$$
2. **Check the output.** Its output on x rises from 0 to 2.5, now correct.
3. **Mistake bound.**
   $$(5/0.5)^2 = 100$$
   mistakes before it finds a separating line.

**Answer:** after the update w = (2, −1.5), and on that data it can make at most 100 mistakes.
:::
:::

::: example
## Two classes have a within-class covariance of $\begin{bmatrix}2 & 1 \\ 1 & 2\end{bmatrix}$. Find Fisher's separation when their means differ by (1, 1), and when they differ by (1, −1).

::: solution
1. **Invert the covariance.**
   $$S_W^{-1} = \frac13\begin{bmatrix}2 & -1 \\ -1 & 2\end{bmatrix}$$
2. **Means differing by (1, 1).**
   $$\begin{aligned} S_W^{-1}\Delta\mathbf m &= \tfrac13(1, 1) \\ J &= \Delta\mathbf m^TS_W^{-1}\Delta\mathbf m \\ &= 2/3 \end{aligned}$$
3. **Means differing by (1, −1).**
   $$\begin{aligned} S_W^{-1}\Delta\mathbf m &= (1, -1) \\ J &= 2 \end{aligned}$$
   three times better, though both mean differences are the same length.
4. **Why.** The noise is correlated along (1, 1), so a difference across that direction stands out against little noise; Fisher's direction finds that automatically.

**Answer:** the separation is J = 2/3 for means differing by (1, 1), and J = 2 for (1, −1).
:::
:::

::: check
given: a = 1 | 3; b = 2 | 4; c = -6 | -2 | 1; x = 1 | 4; y = 0 | 2
answer: (a*x + b*y + c)/sqrt(a^2 + b^2)
tolerance: 0.5%

## A linear classifier has w = ({a}, {b}) and $w_0$ = {c}. What's the signed distance of the point ({x}, {y}) from its boundary?

::: right
Right. $(\mathbf{w}^T\mathbf{x} + w_0)/\|\mathbf{w}\|$ = {answer}.
:::

::: misses
- [a*x + b*y + c] Divide by $\|\mathbf{w}\|$ to get a distance.
- [any] Not yet. Normalize the discriminant.
:::
:::

::: check
given: m = 1 | 2; n = 4 | 8; v = 0.5 | 2; u = 1 | 3
answer: (n - m)^2/(v + u)
tolerance: 0.5%

## Two classes project onto a line with means {m} and {n} and variances {v} and {u}. What's Fisher's separation J?

::: right
Right. $(m_2 - m_1)^2/(s_1^2 + s_2^2)$ = {answer}.
:::

::: misses
- [(n - m)/(v + u)] Square the difference of the means.
- [any] Not yet. Squared mean difference over summed variances.
:::
:::

::: check
given: p = 1 | 3; q = 1 | 2; a = 0.5 | 1 | 4; b = 2 | 9
answer: atan((q/b)/(p/a))*180/pi
unit: °
tolerance: 0.5%

## Class means differ by ({p}, {q}) and the within-class scatter is diagonal with variances {a} and {b}. At what angle from the first axis is Fisher's direction?

::: right
Right. $S_W^{-1}\Delta\mathbf{m}$ = ({p}/{a}, {q}/{b}), at {answer}°.
:::

::: misses
- [atan(q/p)*180/pi] That's the direction between the means; Fisher scales each component by the inverse variance.
- [any] Not yet. $\mathbf{w} \propto S_W^{-1}\Delta\mathbf{m}$.
:::
:::

::: check
given: p = 1 | 3; q = 1 | 2; a = 0.5 | 1 | 4; b = 1 | 9
answer: p^2/a + q^2/b
tolerance: 0.5%

## With the same classes (Δm = ({p}, {q}), variances {a} and {b}), what separation does Fisher's direction achieve?

::: right
Right. $\Delta\mathbf{m}^TS_W^{-1}\Delta\mathbf{m}$ = {answer}.
:::

::: misses
- [any] Not yet. $\sum\Delta m_i^2/\sigma_i^2$.
:::
:::

::: check
given: w = 1 | -1; v = -2 | 2; x = 2 | 3; y = 1 | -1; t = 1 | -1; e = 0.5 | 1
answer: v + e*t*y
tolerance: 0.5%

## A perceptron with w = ({w}, {v}) and learning rate {e} updates on the misclassified point ({x}, {y}) with target {t}. What's the new second component of w?

::: right
Right. $w_2 + \eta tx_2$ = {answer}.
:::

::: misses
- [v - e*t*y] The update adds $\eta t\mathbf{x}$.
- [any] Not yet. $\mathbf{w} \leftarrow \mathbf{w} + \eta t\mathbf{x}$.
:::
:::

::: check
given: R = 1 | 5 | 20; g = 0.1 | 0.5 | 2
answer: (R/g)^2
tolerance: 0.5%

## Separable data lie within radius {R} with margin {g}. At most how many mistakes does the perceptron make?

::: right
Right. $(R/\gamma)^2$ = {answer}.
:::

::: misses
- [R/g] Novikoff's bound is the square of the ratio.
- [any] Not yet. Novikoff's theorem.
:::
:::

::: check
kind: parts
given: a = 1 | 2 | 4; b = 1.5 | 3; c = -0.6 | 0.9; p = 1 | 2; q = -1.5 | 0.5 | 3

## Two classes share a within-class covariance $\begin{bmatrix}a & c \\ c & b\end{bmatrix}$ with a = {a}, b = {b}, c = {c}, and their means differ by ({p}, {q}).

::: part
answer: (b*p^2 - 2*c*p*q + a*q^2)/(a*b - c^2)
tolerance: 0.5%

### What separation J does Fisher's direction achieve?

::: misses
- [p^2/a + q^2/b] That ignores the correlation c.
- [any] $J = \Delta\mathbf m^TS_W^{-1}\Delta\mathbf m$.
:::
:::

::: part
answer: (p^2 + q^2)^2/(a*p^2 + 2*c*p*q + b*q^2)
tolerance: 0.5%

### What separation would projecting onto the mean difference itself give?

::: misses
- [any] $J = (\Delta\mathbf m^T\Delta\mathbf m)^2/(\Delta\mathbf m^TS_W\Delta\mathbf m)$ for the direction Δm.
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3, c = -3..3, x = -5..5, y = -5..5

## A linear classifier has weights (a, b) and bias c; consider the point (x, y).

::: step
answer: a*x + b*y + c

### What is its activation?
:::

::: step
answer: (a*x + b*y + c)/sqrt(a^2 + b^2)

### What is the point's signed distance from the boundary?
:::

::: right
Right. The activation measures distance in units of $1/\|\mathbf w\|$, which is why the SVM's margin is $1/\|\mathbf w\|$.
:::
:::

::: explain
## Why isn't the best direction for separating two classes simply the line joining their means?

::: model
Projecting onto the line between the means maximizes how far apart the projected means are, but it ignores how widely each class spreads along that line. If the classes are elongated in that direction, their projections overlap heavily despite the distant means. Fisher's criterion divides the squared separation by the projected spread, so it prefers a direction along which the classes are narrow, even at the cost of some mean separation. That's why the solution involves the inverse of the within-class scatter: it tilts the direction away from axes of large variance and toward axes where the classes are tight.
:::
:::

::: practice

::: check
given: a = -2 | 3; b = 1 | 5; c = -4 | 2; x = -1 | 3; y = 1 | 4
answer: (a*x + b*y + c)/sqrt(a^2 + b^2)
tolerance: 0.5%

## Signed distance: w = ({a}, {b}), $w_0$ = {c}, point ({x}, {y})?

::: misses
- [any] $(\mathbf{w}^T\mathbf{x} + w_0)/\|\mathbf{w}\|$.
:::
:::

::: check
given: p = 0.5 | 4; q = 0.5 | 3; a = 0.2 | 5; b = 0.3 | 6
answer: p^2/a + q^2/b
tolerance: 0.5%

## Fisher's J: Δm = ({p}, {q}), variances {a}, {b}?

::: misses
- [any] $\sum\Delta m^2/\sigma^2$.
:::
:::

::: check
given: R = 2 | 50; g = 0.05 | 3
answer: (R/g)^2
tolerance: 0.5%

## Perceptron mistake bound: R = {R}, γ = {g}?

::: misses
- [any] $(R/\gamma)^2$.
:::
:::
:::
