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.
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:
| Type | The answer is | Toko Kita example |
|---|---|---|
| classification | a category | will this customer churn? (yes/no); which product category is this review about? |
| regression | a number | how 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:
- Clustering: group customers into segments with similar behaviour (bargain hunters, loyal regulars, big business buyers), without telling it what the segments are.
- Dimensionality reduction: summarise 50 columns into 2 or 3 that keep most of the information, for charts or as input to other models.
- Anomaly detection: flag orders that look unlike all the others (possible fraud).
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:
- The rule is known and stable: VAT is 11%; shipping is free above Rp 500.000. Writing the rule is clearer, exact and free.
- You don't have enough labelled examples: a few dozen rarely suffice; hundreds to thousands is typical.
- Mistakes are costly and must be explained: a loan rejection may need a reason a person can check.
- A simple baseline already works: always compare a model with "the obvious rule". If the model barely beats it, it isn't worth the complexity.
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
- ML finds rules (models) from examples, instead of you writing them.
- A model has parameters; training searches for the parameters that fit the examples best.
- Supervised = examples with labels: classification (categories) or regression (numbers).
- Unsupervised = no labels: clustering, dimensionality reduction, anomaly detection.
- Use plain rules when they're known, data is scarce, or explanations are mandatory. Always beat a simple baseline.