Sub topic 1
Start with the question: what are you predicting and why?
Prerequisites
This chapter assumes you know basic Python and pandas, and the supervised learning vocabulary: features, labels, and training and test data. You should also be comfortable with scikit-learn's fit and predict. No model code is needed yet. The work here happens before you pick any algorithm.
Why framing comes first
Model choice depends on the question. Two teams can hold the same customer table and need different models, metrics, and validation schemes. A mistake in framing cannot be fixed by tuning later. If you predict the wrong target, a very good model still answers the wrong question.
Answer four questions in writing before you fit anything.
- What is the target, and how exactly is it defined?
- What decision will the prediction change?
- When will the prediction be made, and what data exists at that moment?
- What does each kind of error cost?
Define the target precisely
"Will the customer churn?" is vague. A usable definition names the window and the rule: "no purchase within 90 days after the prediction date." Changing the window changes the labels, the class balance, and the best model.
The target type then points to the task family.
| Target | Task | Example |
|---|---|---|
| Two categories | Binary classification | Churn or no churn |
| Several categories | Multiclass classification | Ticket routing |
| A number | Regression | Next month's spend |
| Counts or rates | Regression, often with a count-aware model | Claims per policy |
Turning a number into a category, such as "high spend" above a cutoff, throws information away. Do it only if the decision really is a yes or no.
Tie the prediction to a decision
Ask what someone will do with the output. If a retention team can call only 200 customers a week, you need a good ranking of customers, not calibrated probabilities for everyone. If a loan officer needs to explain a rejection, interpretability becomes a requirement. These answers feed directly into the metric chapter and the practical-constraints chapter later.
Check what is known at prediction time
Every feature must be available when the prediction is made. A column such as refund_issued may be filled in only after a customer churns. Using it gives a model that looks excellent in validation and fails in production. This is a form of leakage, covered in the validation chapter. Frame the timeline now to avoid it.
Inspect the target
Before modeling, look at the target. This short check shows the task type and the class balance.
import pandas as pd
df = pd.DataFrame({
"monthly_spend": [20.0, 35.5, 12.0, 80.0, 41.0, 15.5, 60.0, 22.0],
"churned": [0, 1, 0, 0, 1, 0, 0, 0],
})
target = df["churned"]
print(target.dtype, target.nunique())
print(target.value_counts(normalize=True).round(2))Two unique values with a 75/25 split or more skewed means binary classification with imbalance. Plain accuracy will mislead here, because predicting "no churn" for everyone already scores high.
Common errors
- Defining the target after looking at which definition gives the best score.
- Including features recorded after the event you predict.
- Treating all errors as equally costly.
- Skipping the baseline question: what does the business do today without a model?
Exercise
A telecom wants to prevent churn. The retention team can contact 500 customers each month. Write the target definition, the task type, and one feature you must exclude because of timing.
Show answer
Target: 1 if the customer cancels service within 60 days after the prediction date, otherwise 0. Task: binary classification, where ranking customers by risk matters because only 500 can be contacted. Exclude a feature such as "cancellationreason" or "finalbill_issued", because it is recorded only after the customer leaves and would not exist at prediction time.