Machine learning fundamentalsMiddleLesson 1076 min read

What machine learning really does

Instead of writing the rule yourself, you give the computer examples and let it find the rule. Learn what that means, and what it does not mean.

Lesson in motion

In 60 seconds

What machine learning really does

Instead of writing the rule yourself, you give the computer examples and let it find the rule. Learn what that means, and what it does not mean.

1/4
In simple words
Your grandmother can tell a ripe mango from an unripe one in a second. Nobody gave her a rule book. She has seen thousands of mangoes, and each time she found out if she was right. Machine learning is like that: we show the computer many examples with the right answers, and it slowly finds a rule that works for most of them.
In normal programming, you write the rule. For example: "if the message contains the word lottery, mark it as spam." The computer follows your rule exactly.
In machine learning, you give the computer examples and the right answers, and a method for searching. The computer tries many possible rules and keeps the one that makes the fewest mistakes on the examples. The rule it finds is called a model.
Normal programming
  • You write the rule by hand
  • Input + rule → answer
  • Good when the rule is clear and short
  • Easy to explain every decision
Machine learning
  • The computer finds the rule from examples
  • Inputs + answers → rule (the model)
  • Good when the rule is fuzzy or has many parts
  • Needs good examples and honest testing

Learning is searching for the best setting

Here is the smallest possible example. We have made-up mango weights, each marked ripe (1) or not ripe (0). We want a rule of the form "ripe if weight is at least T grams". Learning means trying every T and keeping the one with the fewest mistakes.
Python: learning a threshold from made-up examplespython
# Made-up examples: (weight in grams, ripe? 1 = yes, 0 = no)
examples = [(150, 0), (170, 0), (180, 0), (200, 1), (190, 0),
            (220, 1), (240, 1), (210, 1), (205, 0), (260, 1)]

best_t, best_mistakes = None, len(examples) + 1
for t in range(140, 281, 10):          # try many possible rules
    mistakes = 0
    for weight, ripe in examples:
        guess = 1 if weight >= t else 0
        if guess != ripe:
            mistakes += 1
    if mistakes < best_mistakes:
        best_t, best_mistakes = t, mistakes

print("Learned rule: ripe if weight >=", best_t, "grams")
print("Mistakes on the examples:", best_mistakes, "out of", len(examples))
Run it. The computer finds a rule with one mistake. No threshold gets all ten right, because one 205-gram mango is not ripe. Real data is like that: there is noise, and no rule is perfect.

The three big families

FamilyWhat you give itWhat it learnsExample
Supervised learningExamples with right answers (labels)To predict the answer for new examplesPredict a house's rent; mark an email as spam or not
Unsupervised learningExamples with no answersGroups or patterns in the dataGroup shop customers by how they buy
Reinforcement learningA world to act in and rewardsWhich actions earn more rewardA program learning to play a game
This track covers the first two. Most machine learning at work is supervised learning.

What machine learning is not

  • It is not magic. A model only finds patterns that are in the examples. If the examples are wrong or one-sided, the model will be too.
  • It does not understand. A model that sorts mangoes has no idea what a mango is. It found a number that separates two groups.
  • It is not always needed. If you can write a clear, short rule, write the rule. It is cheaper and easier to explain.
  • Past patterns can break. A model learned from last year's data may fail when things change, for example a new variety of mango.
Note
Large language models are also machine learning. They use the same basic idea (learn from examples by reducing mistakes) at a much bigger scale. The classic methods in this track are still used every day for tables of numbers, and they teach the habits that matter everywhere: good data, fair tests and honest numbers.
Try this. In the code, change the 205-gram mango to ripe (1). How many mistakes does the best rule make now?

Watch and read more

Lab

A hand-made rule and a learned straight line for made-up study data, with their average errors side by side.

~20 min

The problem

The starter makes made-up data: hours studied and marks for 30 imaginary students. It already has a hand-made rule. Your job:

  1. Run the starter and read the hand rule's average error.
  2. Use np.polyfit(hours, marks, 1) to learn a straight line. Store the results in slope and intercept.
  3. Compute learned_mae: the mean absolute error of the learned line on the same data.
  4. Store the learned line's prediction for 7 hours in pred_7.
  5. Compare the two errors. Write one sentence on why the learned line does better.

Practice lab

Edit the code and run it. Python runs in your browser — nothing is sent to our server.

Output

No output. Add print(...) to see values.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1The learned line has a smaller error on these 30 students. Does that prove it will do better on new students?Reveal

No. It was chosen to fit these exact 30 points, so its error here is a little optimistic. To know how it does on new students, you must test it on students it did not see during fitting. That is the idea behind train and test sets, later in this track.

Q2Why is the data clipped to 0–100, and could that bend the straight line?Reveal

Marks cannot go below 0 or above 100, so clipping keeps the made-up data realistic. Near the edges, clipping flattens the points, and a straight line cannot bend to follow that. Here few points hit the limits, so the effect is small, but it is a reminder that a straight line is a choice, not a law.

Please sign in to continue.

Questions people ask

Is machine learning the same as AI?

Machine learning is one way of building AI, and today it is the main one. AI is the bigger idea of computers doing tasks that seem to need intelligence. Some older AI used only hand-written rules.

Do I need a lot of maths?

To use the methods in this track, you need school-level ideas: averages, a straight line, percentages. To invent new methods you need more, but you can go far with good habits and clear thinking.

How many examples do I need?

There is no fixed number. Simple patterns with little noise need few examples. Complex patterns need many. The honest answer comes from testing: if the model does well on examples it has never seen, you have enough for now.

Why not just write rules by hand every time?

For some problems the rule has hundreds of small parts, like recognising handwriting. Nobody can write that by hand. Learning from examples finds those parts for you.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress