The machine learning workflow
Good machine learning starts with a good question. This module frames problems properly and gets your first model running in minutes.
Learning objectives
- Frame a problem with a decision, target, available data and success measure
- Distinguish classification from regression
- Outline the end-to-end ML workflow
- Use scikit-learn's create–fit–predict pattern
- Compare a model against a baseline
This course uses Python, pandas and scikit-learn, all pre-installed in Google Colab. If you're new to them, take Python for AI and Data Quality & Preparation first. The non-mathematical background is in How AI and LLMs Work.
1.1 Frame the problem first
Most failed ML projects fail at the start: the wrong question, the wrong target, or no clear way to measure success. Before writing code, answer:
- What decision will the model support? e.g. "which customers should the retention team call this week?"
- What exactly will it predict? A category (classification) or a number (regression)?
- What data will be available at prediction time? (No peeking into the future.)
- How will success be measured — technically and in business terms?
- What's the simplest baseline? A rule of thumb or "always predict the average".
1.2 Classification or regression?
Classification — predict a category
Will this customer churn (yes/no)? Is this transaction fraudulent? Which product category is this?
Regression — predict a number
What will this customer spend next year? How many units will we sell? How long will delivery take?
1.3 The machine learning workflow
- Frame the problem and success measure.
- Get and prepare data (cleaning, features).
- Split into training and test sets.
- Build a baseline.
- Train models and compare them with cross-validation.
- Evaluate the chosen model once on the test set.
- Interpret, check fairness, document.
- Deploy, monitor and retrain when needed.
1.4 scikit-learn's simple pattern
Every scikit-learn model follows the same three steps — which makes it easy to try many models:
model = SomeModel(settings) # 1. create
model.fit(X_train, y_train) # 2. learn from training data
predictions = model.predict(X_test) # 3. predict on new data
X holds the features (inputs, one row per example); y holds the target (what you want to predict).
1.5 The practice dataset
You'll use a synthetic customer dataset with a hidden pattern built in, so you can see whether models find it. Run this in a notebook:
Python · practice data: synthetic customers (run this first)import numpy as np
import pandas as pd
rng = np.random.default_rng(42)
n = 2000
customers = pd.DataFrame({
"tenure_months": rng.integers(1, 60, n),
"monthly_spend": rng.normal(40, 15, n).clip(5, 120).round(2),
"support_tickets": rng.poisson(1.2, n),
"on_promo": rng.integers(0, 2, n),
"region": rng.choice(["UK", "IE"], n, p=[0.75, 0.25]),
})
# Hidden "true" pattern: short tenure and many tickets raise churn risk
logit = (1.2 - 0.06 * customers["tenure_months"] + 0.55 * customers["support_tickets"]
- 0.02 * customers["monthly_spend"] + 0.4 * customers["on_promo"])
customers["churned"] = (rng.random(n) < 1 / (1 + np.exp(-logit))).astype(int)
# Annual value we might want to predict (regression)
customers["annual_value"] = (customers["monthly_spend"] * 12 * (1 - 0.3 * customers["churned"])
+ rng.normal(0, 40, n)).round(2)
print(customers.head())
print("Churn rate:", customers["churned"].mean().round(3))
1.6 Your first model in ten lines
Python · first classifier vs a baselinefrom sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.dummy import DummyClassifier
features = ["tenure_months", "monthly_spend", "support_tickets", "on_promo"]
X, y = customers[features], customers["churned"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
print("Baseline accuracy:", round(baseline.score(X_test, y_test), 3))
print("Model accuracy: ", round(model.score(X_test, y_test), 3))
Always compare with a baseline. A model that can't beat "always predict the most common answer" isn't learning anything useful.
Key takeaways
- Start with the decision the model supports and how success is measured.
- Classification predicts categories; regression predicts numbers.
- Split, baseline, train, compare, test once, interpret, deploy, monitor.
- Every scikit-learn model: create, fit, predict.
- Always beat a baseline.