DataLens

What ML is: supervised vs unsupervised

See machine learning as "finding a rule from examples", tell supervised from unsupervised problems, and know when not to use ML at all.

Level: Foundations · 7 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

The idea

Normal programming: you write the rules, the computer applies them.

rules + data  ──▶  answers

Machine learning flips it: you give the computer examples with answers, and it finds the rules.

data + answers  ──▶  rules (a "model")

Toko Kita wants to know which customers are about to stop buying ("churn"), so marketing can send them a voucher. You could write a rule by hand: "customers who haven't ordered for 60 days will churn". But is 60 right? Does it matter how many orders they placed, or whether they use the app? Machine learning looks at hundreds of past customers, where we know who churned, and finds the rule that best separates churners from loyal customers.

The data

This track uses churn.csv: 400 past customers, their behaviour, and whether they churned in the following 90 days:

import pandas as pd

churn = pd.read_csv("churn.csv")
print(churn.head())
print(churn.shape)
print(churn["churned"].mean().round(3))
   customer_id  tenure_months  orders_90d  avg_order_value  days_since_last_order  support_tickets  \
0         1001             12           7           163000                     47                1
1         1002             33           4           472000                     73                1
2         1003              5           0           124000                     14                0
3         1004             11           2           241000                     14                1
4         1005             16           6           166000                     31                2

    segment  app_user  churned
0  Consumer         0        0
1  Business         0        0
2  Business         0        1
3  Consumer         0        1
4  Consumer         0        0
(400, 9)
0.335

Each row is one example (a customer). The columns describe them, and churned is the answer we want to predict (1 = churned). A third of these customers churned.

Learning is searching for a good rule

Let's make the idea concrete with NumPy. A hand-written rule: "churn if more than 60 days since the last order". How often is it right?

import numpy as np

days = churn["days_since_last_order"].to_numpy()
actual = churn["churned"].to_numpy()

predicted = (days > 60).astype(int)
print("accuracy:", (predicted == actual).mean().round(3))
accuracy: 0.72

Now let the data pick the threshold: try every value and keep the one with the best accuracy.

best_threshold, best_accuracy = None, 0
for threshold in range(1, 120):
    accuracy = ((days > threshold).astype(int) == actual).mean()
    if accuracy > best_accuracy:
        best_threshold, best_accuracy = threshold, accuracy
print(best_threshold, round(best_accuracy, 3))
43 0.742

That loop is machine learning in miniature: a family of possible rules (days > threshold), a way to score them (accuracy), and a search for the best one. Real algorithms search much bigger families (combining all the columns, with weights) far more cleverly, but the idea never changes.

Key idea

A model is a rule with adjustable settings (its parameters). Training means adjusting the parameters so the rule fits the known examples as well as possible.

Supervised learning: examples with answers

When your examples come with the answer (the label), it's supervised learning: the answers "supervise" the search. Two kinds:

TypeThe answer isToko Kita example
classificationa categorywill this customer churn? (yes/no); which product category is this review about?
regressiona numberhow many coffee units will we sell tomorrow? how much will this customer spend next month?

The question to ask: "is the thing I'm predicting a category or a quantity?"

Unsupervised learning: no answers, find structure

Sometimes there's no label, only data. Unsupervised learning finds structure on its own:

The results need human interpretation: the algorithm says "these 80 customers are similar", and you work out why.

When not to use ML

Machine learning is powerful and over-used. Prefer a plain rule or a SQL query when:

That last point matters most. "Predict that nobody churns" is right for 66.5% of these customers. Any model must beat that baseline to be useful.

Why this matters for clean code

Thinking of a model as "a function with parameters, chosen to fit examples" demystifies ML. It's the same discipline as the rest of this course: clear inputs (features), a clear output (the prediction), a measurable goal (the score), and a simple baseline to beat. Teams that skip the baseline end up maintaining complicated models that a one-line rule would have matched.

Recap

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: Features, labels, training vs test data →