Section 1: How machine learning works
The workflow and our first model
Let us train a first model end to end. We use the subscription dataset: one row per customer with features such as tenure in months, monthly charge, plan type and support tickets, and a target column churned.
Load and look
import pandas as pd
df = pd.read_csv("data/subscriptions.csv")
df.head()
df["churned"].value_counts(normalize=True)
About 18 percent of customers churned. Remember this number. A useless model that always predicts "not churned" would be 82 percent accurate, so accuracy alone will mislead us.
Split before anything else
from sklearn.model_selection import train_test_split
X = df.drop(columns=["churned", "customer_id"])
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
The test set is locked away until the very end. Every decision, including preprocessing, feature choices and model tuning, uses only the training data. stratify=y keeps the churn proportion equal in both sets. We dropped customer_id because an ID carries no real signal and could let a model memorise individuals.
A baseline
Always start with a baseline to know what "better" means:
from sklearn.dummy import DummyClassifier
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
baseline.score(X_test, y_test)
A first real model
For now, use only numeric columns:
from sklearn.linear_model import LogisticRegression
num_cols = ["tenure_months", "monthly_charge", "support_tickets"]
model = LogisticRegression(max_iter=1000)
model.fit(X_train[num_cols], y_train)
Every scikit-learn model shares the same interface: fit to learn from data, predict to make predictions, and for classifiers predict_proba to get probabilities. This consistency is one of the library's great strengths: you can swap algorithms with one line.
Evaluate properly
from sklearn.metrics import classification_report
print(classification_report(y_test, model.predict(X_test[num_cols])))
The report shows precision and recall for each class. Recall for churners tells us what share of real churners the model catches. We will explore these metrics in depth later.
The workflow
- Define the question and the target.
- Split the data and lock away the test set.
- Build a baseline.
- Prepare features and train models, comparing them on validation data.
- Evaluate the final choice once on the test set.
- Interpret, document and deploy.
Every project in this course follows these steps.