Index Ai Academy
Phase 3 · Machine learning · Foundations

What machine learning is

Programs that learn rules from examples instead of being told them.

About 20 minutes · Free lesson

Start this lesson

Why it matters

You've used machine-learned models all through Phase 1: every LLM is one. This phase opens the box on the classic kinds. They still run a lot of quiet business machinery. They predict which customers will leave, score which leads are likely to buy, and estimate what a customer will spend over time. They flag fraud and recommend products. They're also the clearest place to learn the ideas that carry over to deep learning and LLMs. Those ideas are training data and generalization (working well on cases the model hasn't seen). They also include overfitting (memorizing instead of learning) and honest testing.

What you'll be able to do

  • Contrast writing rules by hand with learning them from examples
  • Name the main kinds of learning: supervised, unsupervised, self-supervised and reinforcement
  • Judge when machine learning is worth it, and when a rule is better
  • See 'learning' as searching for the settings that make the fewest mistakes

What it covers

  1. Rules in, or rules out
  2. Learning is searching
  3. Worked example: learning a cutoff by hand
  4. Kinds of learning
  5. When machine learning is worth it

Rules in, or rules out

Think about how a child learns what a dog is. Nobody hands them a rulebook: "four legs, fur, a tail." That rulebook would fail anyway, because cats have all of those too. Instead, people point and say "dog" or "not a dog," over and over. The child works out the pattern alone.

Computers can work either way.

In ordinary programming, you write the rules. Say you run an online shop. You might write: "if a customer hasn't ordered in 90 days, flag them as at risk of leaving." Customer data goes in, and answers come out. A customer who stops buying has churned, and the loss of customers is called churn.

In machine learning, you don't write the rule. You give the computer past examples together with the answers. Here that means past customers, and whether each one churned. The computer finds the rule itself:

Traditional programming:   rules + data      →  answers
Machine learning:          data + answers    →  rules (a model)

The rule it finds is called a model. A model is a function with some adjustable numbers inside it. Those numbers are its parameters. Think of them as dials, like the dial on an oven. Training means turning the dials until the model's answers match the known answers as closely as possible. Then you use the model on new customers, whose answers you don't know yet.

In plain words: ordinary programming turns rules into answers. Machine learning turns examples with answers into rules.

Quick check
Your shop's rule is: flag a customer as at risk if they haven't ordered in more than 90 days. If a computer learned this rule from past customers, which part would it have learned?
  1. The number 90 (the cutoff)
  2. Whether each past customer churned
  3. Nothing: rules can't be learned
  4. The list of past customers
Show the answer

The number 90 (the cutoff)

The cutoff is the adjustable number, the parameter. The past customers are the data and their churn records are the answers. Training picks the number.

Learning is searching

Here is learning in its simplest form. Why 90 days? Someone guessed. Instead of guessing, you can try lots of cutoffs. Check each one against past customers and count how often it's right. Then keep the cutoff that scores best.

To score a cutoff you need its accuracy: the share of predictions that turned out right.

accuracy = correct predictions ÷ all predictions

Say a cutoff gets 5 of 6 customers right. Its accuracy is 5 ÷ 6, about 0.83, or 83%.

In plain words: accuracy is your hit rate. Count the right answers and divide by the number of guesses.

The code below loads 600 past customers from the course's practice API. It scores the 90-day rule. Then it tries 78 cutoffs, from 10 days up to 395 days in steps of 5, and keeps the best one:

import numpy as np
import requests

customers = requests.get("https://api.gradient.test/v1/customers").json()["data"]
days = np.array([c["days_since_last_order"] for c in customers])
churned = np.array([c["churned"] for c in customers])

def accuracy(cutoff):
    predicted = days > cutoff              # predict churn if the gap is longer than the cutoff
    return np.mean(predicted == churned)

print("hand-picked rule (90 days):", round(accuracy(90), 3))
cutoffs = np.arange(10, 400, 5)
scores = [accuracy(c) for c in cutoffs]
best = cutoffs[int(np.argmax(scores))]
print("learned cutoff:", best, "accuracy:", round(max(scores), 3))
print("always predict 'stays':", round(np.mean(churned == 0), 3))
It runs right here in your browser, with nothing to install.

Here is what the main lines do:

  • days > cutoff gives True or False for each customer. True means "we predict this one churns."
  • predicted == churned checks each prediction against what really happened. Python counts True as 1 and False as 0, so they compare cleanly.
  • np.mean of a list of True and False values gives the share that are True. That's the accuracy.
  • np.argmax(scores) gives the position of the highest score. So best is the winning cutoff.

The hand-picked 90-day rule is right 73.2% of the time. The search finds a cutoff of 295 days, which is right 82.5% of the time. That's a model with one parameter, trained by trying candidates. Real models have many parameters. They use gradient descent (Phase 2) instead of trying everything. But the idea is the same: adjust the parameters to make fewer mistakes on known examples.

Now look at the last line. Predict "stays" for every customer, with no model at all, and you're right 80.7% of the time. That's because most customers stay. This do-nothing score is a baseline. The 90-day rule actually does worse than doing nothing. The learned cutoff beats the baseline by less than 2 points. That's far less impressive than "82.5% accurate" sounds. You'll learn better ways to measure this in the evaluation lessons.

Watch out: before you praise a model's accuracy, ask what the baseline scores.

Go deeper Why big models can't try every setting

Trying every candidate works for one parameter. Here it took 78 tries. Two parameters with 78 candidates each need 78 × 78 = 6,084 tries, one for every pair. Ten parameters need 78 multiplied by itself ten times. That's about 8 billion billion tries (an 8 followed by 18 zeros). Modern LLMs have billions of parameters.

Gradient descent avoids this. It doesn't test every combination. It works out which way to nudge each parameter to cut the errors. Then it takes a small step and repeats. To do that, it needs a smooth score for how wrong the model is: the loss you met in Phase 2. The loss functions lesson, later in this phase, looks at them closely.

Quick check
A model predicts whether customers will cancel a subscription. It's 85% accurate. In this data, 90% of customers don't cancel. What's the fair verdict?
  1. It's good, because 85% is a high score
  2. It's 5 points better than the baseline
  3. There's nothing to compare it with
  4. It's worse than always predicting 'doesn't cancel', which scores 90%
Show the answer

It's worse than always predicting 'doesn't cancel', which scores 90%

Always guessing the most common answer scores 90% here. A model at 85% makes more mistakes than doing nothing.

Worked example

Worked example: learning a cutoff by hand

A small online coffee shop has six past customers. For each one, you know how many days had passed since their last order. You also know whether they churned (1) or stayed (0).

Days since last order10254070105130
Churned000101

Try three cutoffs: 30, 60 and 100 days. Predict "churn" when the gap is longer than the cutoff. Count the right answers for each. Then compare with always predicting "stays."

days    = [10, 25, 40, 70, 105, 130]
churned = [0,  0,  0,  1,  0,   1]

for cutoff in [30, 60, 100]:
    right = 0
    for d, c in zip(days, churned):
        guess = 1 if d > cutoff else 0
        if guess == c:
            right += 1
    print(cutoff, right, "of", len(days))

print("always 'stays':", churned.count(0), "of", len(days))
In the lesson you can change this and run it.

It prints 30 4 of 6, 60 5 of 6, 100 4 of 6 and always 'stays': 4 of 6.

Check the 30-day cutoff by hand. It predicts churn for the customers at 40, 70, 105 and 130 days. It's right about 70 and 130, and wrong about 40 and 105. It's also right about 10 and 25, which it predicts will stay. That's 4 of 6.

The 60-day cutoff predicts churn for 70, 105 and 130. Only 105 is wrong, so it scores 5 of 6. The 100-day cutoff predicts churn for 105 and 130. It's wrong about 105 and misses 70, so it scores 4 of 6.

Of the three, 60 days wins, with accuracy 5 ÷ 6, about 0.83. The baseline is 4 ÷ 6, about 0.67. Even the best cutoff makes one mistake: the customer at 105 days came back. Real data is messy like that.

Quick check
What does this print?
import numpy as np
days = np.array([10, 60, 90, 30])
churned = np.array([0, 1, 0, 0])
print(np.mean((days > 50) == churned))
  1. 0.75
  2. 0.25
  3. 0.5
  4. 1.0
Show the answer

0.75

The cutoff 50 predicts churn for 60 and 90. It's right about 60 and wrong about 90. It's also right about 10 and 30, which it predicts will stay. That's 3 of 4, so 0.75.

Kinds of learning

Machine learning comes in four main kinds. They differ in what the computer gets to learn from.

  • Supervised learning is like studying with flashcards that have the answer on the back. Each example comes with the right answer, called a label. "This customer churned" is a label.
  • Unsupervised learning is like sorting a big box of mixed buttons with no instructions. There are no labels. The computer looks for groups and patterns on its own.
  • Self-supervised learning is like covering the next word of a sentence and guessing it. The answer is hidden in the data itself, so nobody has to write labels.
  • Reinforcement learning is like training a dog with treats. The learner tries actions and gets rewards for good ones. It learns by trial and error.
KindWhat it learns fromMarketing example
Supervisedinputs paired with the right answer (a label)predict churn from customer history
Unsupervisedinputs only, no labelsfind customer segments
Self-supervisedraw data where the "label" is part of the dataan LLM predicting the next word of web text
Reinforcementrewards for actions, by trial and errorchoosing which offer to show, learning from clicks

Large language models use several kinds. During pretraining they're self-supervised. In any web text, the next token is a free label. After that, they're fine-tuned with supervised examples and with reinforcement learning from feedback. This phase covers supervised and unsupervised learning, the base that all the rest builds on.

Try it now

Sorting these by what data you have is how you start most real projects.

Quick check
You want to group your customers into natural segments, but you have no labels saying which segment anyone belongs to. What kind of learning is that?
  1. Supervised
  2. Unsupervised
  3. Reinforcement
  4. It isn't machine learning
Show the answer

Unsupervised

Finding structure without labels is unsupervised learning (clustering, later in this phase).

When machine learning is worth it

Machine learning isn't always the right tool. Use it when:

  • the pattern is too complex or fuzzy to write down (what makes a lead likely to buy?),
  • you have plenty of examples with known outcomes,
  • some mistakes are acceptable, and
  • the pattern changes over time, so you'll want to relearn it from new data.

Use a plain rule when a simple rule works well, or when you have little data. Also use a rule when you need a decision you can fully explain and guarantee. "Never email unsubscribed customers" is a rule, not a prediction. A model that's right 99% of the time would still be wrong 1% of the time. Here, even one wrong email breaks a promise.

The workflow

Real machine learning projects follow roughly the same six steps:

1. Frame the question and the metric    which customers will churn next quarter? judged how?
2. Gather data with known outcomes      past customers and what happened to them
3. Prepare features                      clean, encode, scale (the next lessons)
4. Train on part of the data             fit the model's parameters
5. Evaluate on data it hasn't seen       the honest number
6. Deploy and monitor                    predictions drift as customers change

A few words here may be new. Features are the inputs a model learns from, such as days since the last order. A metric is the score you'll judge the model by, such as accuracy. To deploy a model is to put it to work on real customers. To monitor it is to keep checking it, because customers change and old patterns fade.

Steps 4 and 5 use different customers on purpose. Testing a model on examples it trained on is like grading students on the exact questions they practiced. They'll look better than they are.

Most of the effort in practice goes into steps 1 to 3 and step 5, not the model itself.

Practice what you just read

  • 2 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 4 review cards for spaced repetition

Start this lesson

Try a quiz question

What does a supervised learning algorithm need that traditional programming doesn't?

  1. Examples with known correct answers
  2. Rules written out by hand, one for each case
  3. A GPU, as ordinary processors can't run it
  4. Data with the answers removed from every row

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson