Classification
About this lesson
Classification predicts a category, called a class, instead of a number. Using six real iris flowers described by two features, petal length and petal width, watch a perceptron learn: it scores each flower, checks its guess against the label, and nudges its weights after every mistake until a straight line, the decision boundary, separates the two classes. Then it sorts flowers it has never seen and we measure its accuracy.
Predict a class
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.
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 classificationRead the lesson step by step
Predict a class
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.
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 classificationFeatures and labels
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.
features: petal length L, petal width W (cm) label: the right answer, from a botanist L W label 3.3 1.0 versicolor 1.4 0.2 setosa 1.7 0.4 setosa 1.3 0.2 setosa 4.7 1.4 versicolor 4.0 1.3 versicolorA score and a threshold
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.
s = w1*L + w2*W + b guess = 1 if s > 0 else -1 w1, w2 weights: how much each feature counts b bias: shifts every score up or down start w1 = 0, w2 = 0, b = 0 every score is 0 -> every guess is setosaWrong? Nudge the weights
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.
flower (3.3, 1.0) label versicolor (y = +1) s = 0*3.3 + 0*1 + 0 = 0 guess -> setosa wrong w1 = 0 + 3.3 = 3.3 w2 = 0 + 1 = 1 b = 0 + 1 = 1Too far: nudge back
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.
flower (1.4, 0.2) label setosa (y = -1) s = 3.3*1.4 + 1*0.2 + 1 = 5.82 guess -> versicolor wrong w1 = 3.3 - 1.4 = 1.9 w2 = 1 - 0.2 = 0.8 b = 1 - 1 = 0The line appears
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.
flower (1.7, 0.4) label setosa (y = -1) s = 1.9*1.7 + 0.8*0.4 + 0 = 3.55 guess -> versicolor wrong w1 = 1.9 - 1.7 = 0.2 w2 = 0.8 - 0.4 = 0.4 b = 0 - 1 = −1 score = 0 on the line 0.2*L + 0.4*W = 1An epoch, then stop
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.
(1.3, 0.2) s = −0.66 setosa right (4.7, 1.4) s = 0.5 versicolor right (4.0, 1.3) s = 0.32 versicolor right epoch 1: 3 mistakes epoch 2: 0 mistakes -> stop final model: w1 = 0.2, w2 = 0.4, b = −1 training accuracy: 6 of 6Classify a new flower
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.
predict(4.6, 1.5) s = 0.2*4.6 + 0.4*1.5 - 1 = 0.52 above 0 -> "versicolor" label (UCI row 55): versicolor rightTest it: accuracy
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.
predict(1.4, 0.3) s = 0.2*1.4 + 0.4*0.3 - 1 = −0.6 not above 0 -> "setosa" right accuracy = correct / total = 2 / 2 = 100% two test flowers prove little: real tests use many
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which of these is a classification task?
Spam or not spam is a choice between categories (classes). The other three predict a number, which is regression.
02The model has w1 = 0.2, w2 = 0.4 and b = −1. A flower has petal length 4.0 and width 1.3. What is its score, and its class?
0.2 × 4.0 + 0.4 × 1.3 − 1 = 0.8 + 0.52 − 1 = 0.32. That is above 0, so the classifier says versicolor.
03During training, the perceptron guesses setosa for a flower whose label is versicolor (y = +1). What does it do?
On a mistake the perceptron adds y times the features to the weights and y to the bias. For versicolor y = +1, so it adds; for a wrong setosa (y = −1) it subtracts.
04What is the decision boundary of this classifier?
Scores above 0 mean versicolor and the rest mean setosa, so the two classes meet where the score is 0. For two features that place is a straight line.
05Why do we measure accuracy on flowers that were kept out of training?
The perceptron kept changing until every training flower was right, so 6 of 6 on them proves little. Held-out test examples show how it does on new data.
Glossary
- Classification
- Predicting which category (class) an example belongs to, such as setosa or versicolor. Regression predicts a number instead.
- Class
- One of the categories a classifier can answer. Two classes make binary classification.
- Feature
- A number that describes an example, such as petal length in centimetres.
- Label
- The right answer attached to a training example: here, the flower's species.
- Weights and bias
- The numbers a linear classifier learns. Each weight says how much a feature counts; the bias shifts every score up or down.
- Decision boundary
- Where the classifier's answer changes from one class to the other: here the line where the score is exactly 0.
- Perceptron
- An early learning rule (Rosenblatt, 1958): on each mistake, add the features times the label (+1 or −1) to the weights.
- Epoch and accuracy
- An epoch is one pass over all the training data. Accuracy is the share of examples a classifier gets right, best measured on held-out test data.
Notes and sources
- 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.
- The eight flowers are real rows of Fisher's 1936 iris measurements (UCI rows 1, 3, 6, 7, 51, 54, 55 and 58). The full data set has 150 flowers of three species.
- The Python program in the lesson is real: run it and it prints "0.2 0.4 -1.0", then "versicolor" and "setosa".
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma): classification, logistic regression and the perceptron
- F. Rosenblatt, The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain, Psychological Review 65(6), 1958
- Cornell CS4780 Machine Learning (K. Weinberger), lecture notes: the perceptron and its convergence
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), chapter 5: machine learning basics
- UCI Machine Learning Repository: Iris (R. A. Fisher, 1936)
- Google Machine Learning Crash Course: Classification (thresholds, accuracy)
- Python Tutorial: for statements and range()
Quiz Unlocks after the last step.