Sub topic 1
Start with the question: framing the problem and the target
Prerequisites
Assume you know basic Python, pandas tables, mean and variance, and the idea of supervised learning.
Frame the problem
Start with a decision, not an algorithm. State what must be predicted, when the prediction is made, and how success will be measured.
The target is the value the model must predict. Features are the input columns available at prediction time. For example, “Which customers will cancel next month?” has a binary target, cancelled. A useful metric might be recall if missing a likely cancellation is costly.
Write down the unit of prediction, such as one customer or one transaction. This prevents accidentally mixing information from different times or entities.
Collect and understand the data
Check where each column came from, its type, its time period, and its meaning. Confirm that every feature would be available when the prediction is made. Look for duplicate rows, impossible values, class imbalance, and changes in data collection.
A dataset might contain numeric columns such as income, categorical columns such as plan, and a target column. Keep a data dictionary that records these meanings.
Explore the data before modeling
Use summaries and plots to examine distributions and relationships. Compare feature distributions across target classes. Count target values. Investigate unusual values rather than deleting them automatically.
Exploration should inform cleaning and feature choices. It should not become repeated test-set inspection, because that can influence decisions using information meant for final evaluation.
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.DataFrame({
"age": [22, 35, 41, 29, 54, 48],
"plan": ["basic", "pro", "basic", "pro", "pro", "basic"],
"cancelled": [1, 0, 1, 0, 0, 1],
})
print(df.info())
print(df["cancelled"].value_counts())
train, test = train_test_split(
df, test_size=0.33, stratify=df["cancelled"], random_state=42
)Clean data and handle missing values
Decide whether missingness means “unknown,” “not applicable,” or an error. Numeric values are often filled with a training-set median. Categorical values can use the most common category or an explicit "missing" label. Encode categories before fitting many models.
Split data safely
Use training data to fit transformations and models. Use validation data to choose among models or settings. Keep the test set untouched until the final evaluation.
Exercise
Where should you fit an imputer when estimating model performance?
Show answer
On each training fold only, then use it to transform that fold's validation rows.