ENع
←Classification
Lesson 04 · Foundations

Classification

12 min9 steps5 questionsBeginner
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 4 · Interactive
01 / 09

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 classification
Loading figure…
or ← →
Read the lesson step by step
  1. 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 classification
  2. Features 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  versicolor
  3. A 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 setosa
  4. Wrong? 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 = 1
  5. Too 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 = 0
  6. The 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 = 1
  7. An 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 6
  8. Classify 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   right
  9. Test 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

Quiz Unlocks after the last step.