Sub topic 1
Start with the question: framing the problem and the target
Prerequisites
This chapter assumes you know basic Python and pandas, and you can read a mean and a distribution. You should also know that supervised learning means learning from rows that already have a known answer.
From a business question to a learning task
A project starts with a decision someone wants to make, not with an algorithm. "Reduce customer cancellations" is a goal. It becomes a machine learning task only when you can state what you will predict, for which rows, and when.
Answer four questions before you touch any data:
- What decision will the prediction change? For example, who gets a retention offer.
- What is one row? For example, one customer at one point in time.
- What is the target? The column the model learns to predict.
- When is the prediction made, and what is known at that moment?
The task type follows from the target:
| Target | Task | Example |
|---|---|---|
| A category | Classification | Will this customer cancel? |
| A number | Regression | How much will they spend next month? |
Defining the target precisely
A vague target gives a vague model. "Churn" needs a time window. A usable definition reads: the customer cancels within 30 days after the snapshot date. You also need to exclude rows where the outcome is already known, such as customers who cancelled before the snapshot.
import pandas as pd
customers = pd.DataFrame({
"customer_id": [1, 2, 3, 4],
"cancelled_on": pd.to_datetime(["2026-02-01", None, "2026-01-10", None]),
})
snapshot = pd.Timestamp("2026-01-31")
horizon = pd.Timedelta(days=30)
# Keep customers still active at the snapshot.
active = customers[
customers["cancelled_on"].isna() | (customers["cancelled_on"] > snapshot)
].copy()
# Target: 1 if they cancel within 30 days after the snapshot.
active["churned"] = (active["cancelled_on"] <= snapshot + horizon).astype(int)
print(active[["customer_id", "churned"]])Customer 3 cancelled before the snapshot, so that row is dropped. Customer 1 gets a 1, and the customers with no cancellation date get a 0.
Choose the success metric early
Pick the metric from the cost of mistakes, not from habit. If missing a churner costs far more than a wasted offer, recall matters more than accuracy. If only 5% of customers churn, a model that always predicts "no" is 95% accurate and useless. Write the metric down now. Later chapters cover metrics in detail, but choosing one first stops you from picking whichever number looks best afterward.
Common errors
- Using information from the future. If a feature is only known after the prediction time, such as "number of support calls during the cancellation process", the model looks excellent in testing and fails in use. Check every feature against the prediction time.
- Treating "no label" as "negative". A missing outcome may mean the event has not happened yet, not that it never will. Make sure every row had the full time window to show its outcome.
- Predicting something nobody can act on. If no decision depends on the output, the model has no value.
- Changing the target halfway through. A new definition means new labels, new baselines and new scores.
Exercise
A bank wants to predict loan defaults. A teammate proposes the target "customer has ever missed a payment", using every payment on record. What is wrong with this definition, and how would you fix it?
Show answer
It has no time window or prediction time, so features can include payments made after the loan decision, which leaks the future. Fix it by choosing a prediction time, such as the loan approval date. Use only information known by then as features. Define the target as a missed payment, or a default, within a fixed period afterward, such as 12 months. Exclude loans that have not yet had the full 12 months to show an outcome.