The task
Linear regression predicts a number. Classification predicts a category, called a class. Our task: is an iris flower a setosa or a versicolor? With just two classes, this is binary classification.
Features are the numbers that describe each example. We use two: petal length and petal width, in centimetres. Each of our six training flowers is a dot at its two numbers, coloured by its label.
Our classifier gives each flower a score: w1 × length + w2 × width + b. Above 0 means versicolor; otherwise, setosa. Training must find the weights w1, w2 and the bias b. They start at 0.
The training loop
The training loop takes one flower, guesses, and checks the label. Versicolor (3.3, 1.0) scores 0, so the guess is setosa: wrong. The fix: add its numbers to the weights, and 1 to b.
Now every flower scores above 0, so everything looks versicolor. Setosa (1.4, 0.2) scores 5.82: wrong. For a setosa, subtract its numbers and 1: w1 = 1.9, w2 = 0.8, b = 0.
Setosa (1.7, 0.4) still scores 3.55, so subtract again: w1 = 0.2, w2 = 0.4, b = −1. Now the map shows a line where the score is exactly 0: the decision boundary between the two classes.
The other three flowers are already right: scores −0.66, 0.5, 0.32. One pass over all the data is an epoch. The second epoch makes no mistakes, so training stops. That is the training loop.
Using the model
A flower the model never saw: length 4.6, width 1.5. Score: 0.2 × 4.6 + 0.4 × 1.5 − 1 = 0.52. Above 0, so the prediction is versicolor: it falls on the versicolor side of the line.
To judge a classifier, test it on labelled flowers kept out of training. (1.4, 0.3) scores −0.6: setosa, right. Accuracy is the share it gets right: here 2 of 2. Real tests use hundreds of examples.
What happens
;; step 1/9 · task
regression predicts a number (a price, a time)
classification predicts a class (spam or not, a digit)
our classes: setosa (y = -1)
versicolor (y = +1)
two classes = binary classification
The learner is the perceptron (Rosenblatt, 1958). On data that a straight line can separate, it is guaranteed to stop; it stops at the first line that works, not the best one. Many classifiers, such as logistic regression, turn the score into a probability with the sigmoid function 1 / (1 + e^−score): a score of 0 becomes 0.5, so "score above 0" and "probability above 0.5" draw the same line.