What Is Machine Learning

How a program can find its own rule from examples instead of being told it. Learn the core words, watch a rule being learned, and see when ML fits.

13 minLesson 1 of 7
On this page
  1. Rules versus examples
  2. The words you will keep seeing
  3. Watch a rule being learned
  4. The code
  5. Three kinds of learning
  6. When machine learning is the wrong tool
  7. Try it yourself
  8. Check yourself
  9. What to practise next

You run a small delivery service, and customers keep asking whether their parcel will be late. You could sit down and write rules. Late if the distance is over some number of kilometres. Unless it is a weekday morning. Unless the driver is new. Every week you find a case your rules got wrong, and every fix makes the rules harder to read.

You already have something better than your guesses: a spreadsheet of past deliveries, each with its distance and whether it actually arrived late. Machine learning means letting the program read that spreadsheet and work out the rule itself.

Rules versus examples

In ordinary programming, you write the rule and the computer applies it. In machine learning, you give the computer examples together with the right answers, and it finds a rule that fits them. You then use that rule on new cases it has never seen.

Ordinary programming data your rule program answers Machine learning data known answers learning a rule
Ordinary code turns data into answers using your rule. Machine learning turns data plus known answers into a rule.

The rule it finds is called a model. A model is just a function: it takes the facts about one case and returns a guess. What makes it "learned" is that some numbers inside it were chosen by looking at the examples, not typed in by you.

The words you will keep seeing

Every lesson in this module uses the same handful of terms. Here they are on the delivery example.

WordMeaningIn the delivery example
ExampleOne past caseOne delivery from the spreadsheet
FeatureA fact you know before the answerThe distance in km
LabelThe right answer for that exampleLate or on time
ParameterA number inside the model that learning setsThe cut-off distance
TrainingChoosing parameters that fit the examplesFinding the best cut-off
PredictionUsing the trained model on a new case"This 7 km delivery will be late"

Watch a rule being learned

Start with the simplest possible model: "a delivery is late if its distance is at least t km". The model has one parameter, t. Training means picking the best value for it.

Here are seven past deliveries, sorted by distance. Notice the 10 km one: it arrived on time even though longer trips are usually late. Real data has cases like that. Maybe the roads were empty that day.

The plan is blunt: try each distance as the cut-off, count how many past deliveries that rule gets wrong, and keep the cut-off with the fewest mistakes.

Find the cut-off with the fewest mistakesUse the arrows or ← → keys
for each candidate t:  mistakes = 0  for each delivery:    guess = late if distance >= t    if guess != actual: mistakes += 1  keep t if it has the fewest mistakes so far
  1. km
    02t
    13
    25
    36
    48
    510
    612
    guess
    0late
    1late
    2late
    3late
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    2
    mistakes
    4
    best t
    2
    fewest
    4

    1Try t = 2. Every delivery is at least 2 km, so the rule says all of them are late. The four on-time deliveries are wrong.

  2. km
    02
    13t
    25
    36
    48
    510
    612
    guess
    0ok
    1late
    2late
    3late
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    3
    mistakes
    3
    best t
    3
    fewest
    3

    2Try t = 3. Now the 2 km delivery is called on time, which is right. Three mistakes left.

  3. km
    02
    13
    25t
    36
    48
    510
    612
    guess
    0ok
    1ok
    2late
    3late
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    5
    mistakes
    2
    best t
    5
    fewest
    2

    3Try t = 5. Two mistakes: the 5 km and 10 km deliveries were on time but the rule says late.

  4. km
    02
    13
    25
    36t
    48
    510
    612
    guess
    0ok
    1ok
    2ok
    3late
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    6
    mistakes
    1
    best t
    6
    fewest
    1

    4Try t = 6. Only the unusual 10 km delivery is wrong. One mistake, the best so far.

  5. km
    02
    13
    25
    36
    48t
    510
    612
    guess
    0ok
    1ok
    2ok
    3ok
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    8
    mistakes
    2
    best t
    6
    fewest
    1

    5Try t = 8. The rule now misses the late 6 km delivery and still gets 10 km wrong. Two mistakes, so 6 stays the best.

  6. km
    02
    13
    25
    36
    48
    510t
    612
    guess
    0ok
    1ok
    2ok
    3ok
    4ok
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    10
    mistakes
    3
    best t
    6
    fewest
    1

    6Try t = 10. Three mistakes: both late deliveries at 6 and 8 km are missed, and 10 km is still called late.

  7. km
    02
    13
    25
    36
    48
    510
    612t
    guess
    0ok
    1ok
    2ok
    3ok
    4ok
    5ok
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    t
    12
    mistakes
    2
    best t
    6
    fewest
    1

    7Try t = 12. The 10 km delivery is finally right, but 6 and 8 km are missed. Two mistakes.

  8. km
    02
    13
    25
    36
    48
    510
    612
    guess
    0ok
    1ok
    2ok
    3late
    4late
    5late
    6late
    actual
    0ok
    1ok
    2ok
    3late
    4late
    5ok
    6late
    learned t
    6
    mistakes
    1
    rules tried
    7

    8Done. The learned rule is "late if at least 6 km", with 1 mistake out of 7. Nobody typed the number 6; it came from the data.

1 / 8

A new order comes in for 7 km. The model says late, because 7 is at least 6. That is a prediction: an answer for a case that was not in the training data.

The model still gets the 10 km delivery wrong, and that is fine. No simple rule explains every quirk in real data. A model that matches every single example has often memorized noise, which is the trap covered in Overfitting and Evaluation.

The code

function learnThreshold(distances, late) {
  let bestT = null;
  let fewest = Infinity;
  for (const t of distances) {         // try each distance as the cut-off
    let mistakes = 0;
    for (let i = 0; i < distances.length; i++) {
      const guess = distances[i] >= t; // the rule: late if at least t km
      if (guess !== late[i]) mistakes++;
    }
    if (mistakes < fewest) {
      fewest = mistakes;
      bestT = t;
    }
  }
  return { threshold: bestT, mistakes: fewest };
}
 
const distances = [2, 3, 5, 6, 8, 10, 12];
const late = [false, false, false, true, true, false, true];
learnThreshold(distances, late); // { threshold: 6, mistakes: 1 }
def learn_threshold(distances, late):
    best_t = None
    fewest = float("inf")
    for t in distances:                # try each distance as the cut-off
        mistakes = 0
        for d, was_late in zip(distances, late):
            guess = d >= t             # the rule: late if at least t km
            if guess != was_late:
                mistakes += 1
        if mistakes < fewest:
            fewest = mistakes
            best_t = t
    return best_t, fewest
 
distances = [2, 3, 5, 6, 8, 10, 12]
late = [False, False, False, True, True, False, True]
learn_threshold(distances, late)  # (6, 1)
TimeO(n²)
SpaceO(1)

n candidate cut-offs, and each one is checked against all n examples.

Trying every option works here because there is one parameter and seven examples. Real models have thousands to billions of parameters, and you cannot try every combination. They need a smarter way to search, which is the subject of How Models Learn.

Three kinds of learning

  • Supervised learning. Every example has a label, like our late or on-time column. Spam filters, price estimates and photo tagging work this way. Most of this module is about it.
  • Unsupervised learning. There are no labels. The program looks for structure on its own, such as groups of customers who shop alike.
  • Reinforcement learning. The program acts, gets a reward or penalty, and adjusts. Game-playing programs and robot control are typical uses.

When machine learning is the wrong tool

Machine learning gives you a rule that is usually right. That is a great trade when the rule is hard to write by hand and an occasional mistake is acceptable. It is a bad trade in other cases.

  • The rule is already known. Sales tax, sorting a list, checking a password. Write the code.
  • You have few examples, or examples without answers. The model can only learn what the data shows.
  • Every answer must be exact and explainable. "Usually right" is not good enough for a bank balance.

Try it yourself

Try it: Rules or learning?

For each task, decide whether you would write the rule by hand or learn it from examples. Give a one-line reason for each.

Show a sample answer
  1. Write it by hand. The rule exists and is exact. Learning it would only add mistakes.
  2. Learn it. Fake reviews have many subtle signs that nobody can list completely, and you can collect examples labelled by moderators.
  3. Learn it. Past tickets with their real resolution times are good examples, and an estimate that is roughly right is still useful.
  4. Write it by hand. Whether 31 April exists is a fact, not a pattern.

The test is the same each time: is the rule hard to write down, do you have examples with answers, and can you live with occasional mistakes? You need yes to all three.

Check yourself

Quick check

In the delivery model, what is the parameter?

The learned rule still gets the 10 km delivery wrong. What should you conclude?

Which task is the worst fit for machine learning?

What to practise next

Trying every value stops working once a model has more than a few parameters. How Models Learn shows the method that replaces it: measure the error with a loss, then walk downhill with gradient descent.