Course page
Log in

You are watching a free preview. Enroll to open all 24 lessons, track progress and ask the course tutor.

Enroll for $59.99

Section 1: How machine learning works

The workflow and our first model

Article 13 min Lesson 2 of 24 Preview

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

  1. Define the question and the target.
  2. Split the data and lock away the test set.
  3. Build a baseline.
  4. Prepare features and train models, comparing them on validation data.
  5. Evaluate the final choice once on the test set.
  6. Interpret, document and deploy.

Every project in this course follows these steps.

← → switch lessons