ML-01-016-P · Primer — A Tiny End-to-End ML Workflow
The idea in one sentence
Every real machine-learning project is the same five moves in the same order — load, split, scale, fit, evaluate — and once you can run them front-to-back on a tiny dataset, you can run them on anything.
Why it exists
When you're new, ML looks like a pile of disconnected tricks: some scaling here, a model over there, an accuracy number that appears from nowhere. It isn't a pile — it's a pipeline, and the pipeline has a shape. You load your data, split it into a part you learn from and a part you keep hidden to grade yourself honestly, scale the numbers so no single feature bullies the rest, fit a model on the learning part, and evaluate it on the hidden part. This ordering isn't a style choice; it's what keeps you from lying to yourself about how good your model really is. Learn the shape once and every future course — bigger data, fancier models, deep learning — is just a richer version of these same five beats.
How it works
We'll predict whether a room is "cozy" from just two numbers: its area and its temperature. First we load a tiny dataset (here, written inline so it runs anywhere). Then we split off 25% as a test set the model never sees during training — that hidden slice is our honesty check. We scale the features with StandardScaler, and the golden rule is that the scaler learns its mean and standard deviation on the training data only, then applies them to the test data — otherwise the test set leaks into training and your score becomes a fantasy. We fit a LogisticRegression on the scaled training data, then evaluate by predicting on the held-out test set and comparing to the true labels with accuracy_score. Read the code as a story from top to bottom — that top-to-bottom order is the workflow.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 1) LOAD — tiny inline dataset: [area, temperature] -> cozy? (1 = yes, 0 = no)
X = np.array([
[12, 22], [14, 21], [11, 23], [13, 20], # small & warm -> cozy (1)
[15, 22], [10, 24], [16, 21], [12, 20],
[45, 15], [50, 14], [42, 16], [48, 13], # big & cold -> not cozy (0)
], dtype=float)
y = np.array([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0])
# 2) SPLIT — hold out 25% the model never trains on (stratify keeps class balance)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y)
# 3) SCALE — learn mean/std on TRAIN ONLY, then apply to both
scaler = StandardScaler().fit(X_train)
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test)
# 4) FIT — train the model on the scaled training data
model = LogisticRegression().fit(X_train_s, y_train)
# 5) EVALUATE — predict on the untouched test set and grade it
pred = model.predict(X_test_s)
print("accuracy:", accuracy_score(y_test, pred)) # -> accuracy: 1.0
The one gotcha
Fit the scaler on the training data only — never on the full dataset. If you call StandardScaler().fit(X) on everything before splitting, the scaler has already peeked at your test rows to compute its mean and standard deviation. That's data leakage: your evaluation score looks great in the notebook and then collapses on real, unseen data. The fix is baked into the order above — split first, then fit the scaler on X_train, and only ever transform (never re-fit) on X_test.
See it — the five beats, in order
flowchart TD
A["1 · LOAD<br/>raw X, y"] --> B["2 · SPLIT<br/>train + hidden test"]
B --> C["3 · SCALE<br/>fit on TRAIN only"]
C --> D["4 · FIT<br/>train the model"]
D --> E["5 · EVALUATE<br/>score on hidden test"]
B -. "test set waits,<br/>never peeked at" .-> E
The one-line takeaway
Machine learning is five moves in one fixed order — load, split, scale, fit, evaluate — and the whole game is keeping the test set honestly hidden until the very last step.
Where you'll meet it
This exact skeleton is the opening act of nearly every ML task you'll ever touch: spam vs. not-spam, will-this-customer-churn, price prediction, image labels. In real projects the middle grows — you'll wrap the scale-and-fit steps into a Pipeline, swap LogisticRegression for stronger models, and grade with more than accuracy — but the shape stays identical. Nail these five beats here and the deep AIM track is just this same rhythm played on a bigger stage.
Named concepts: load, split, scale, fit, evaluate; workflow order; putting it together
