Ten Principles for Studying and Practising ML
Machine learning has many algorithms and libraries, and they change every year. The habits that make ML work reliably change much more slowly. These ten principles run through the whole track; each one guards against a mistake that experienced practitioners have learned to fear. Two of them are demonstrated in code below.
1. Start with data, not model complexity
A sophisticated model cannot reliably make up for wrong labels, a skewed sample or unusable features. Before trying a bigger model, look at the data: its distributions, its missing values, a few dozen raw examples, the labels of the examples the model gets wrong. The Data for ML course is entirely about this.
2. Understand the problem before selecting an algorithm
Write down the input, the target, the constraints (latency, cost, explainability, privacy) and the success metric before writing any model code. A model that optimises the wrong metric is a precise answer to the wrong question. The , , checklist from What Is Machine Learning? is a good start.
3. Build a simple baseline first
Always know what "no skill" and "simple skill" score on your data. A baseline that predicts the most common class, then a simple model, sets the bar every complex model must clear.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
for name, model in [
("baseline: always the majority class", DummyClassifier(strategy="most_frequent")),
("simple: scaled logistic regression", make_pipeline(StandardScaler(), LogisticRegression())),
("complex: gradient boosting", GradientBoostingClassifier(random_state=0)),
]:
model.fit(X_train, y_train)
print(f"{name:37} validation accuracy {model.score(X_val, y_val):.3f}")baseline: always the majority class validation accuracy 0.626
simple: scaled logistic regression validation accuracy 0.959
complex: gradient boosting validation accuracy 0.942
Always answering "benign" already scores 0.626, so 0.626 is the floor, not 0. The simple scaled logistic regression (0.959) does as well as gradient boosting (0.942) here; on 171 validation rows the difference of three rows is within the noise. Without the baseline and the simple model, a 0.942 might have looked like the result of the more complex model's sophistication.
4. Prevent data leakage
Leakage is information from outside the training data, often from the test data or from the future, that sneaks into training and makes the evaluation too optimistic. A classic case: selecting the "best" features using all the data before cross-validation. Here the data is pure noise, so no honest method can beat 50 %.
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
# Pure noise: 100 rows, 2000 random features, random labels. No model can beat 50 % honestly.
rng = np.random.default_rng(4)
X = rng.normal(size=(100, 2000))
y = rng.integers(0, 2, size=100)
# Leaky: choose the 20 "best" features using ALL rows, then cross-validate.
X_leaky = SelectKBest(f_classif, k=20).fit_transform(X, y)
leaky = cross_val_score(LogisticRegression(), X_leaky, y, cv=5).mean()
# Honest: feature selection is part of the pipeline, refitted inside each training fold.
pipe = make_pipeline(SelectKBest(f_classif, k=20), LogisticRegression())
honest = cross_val_score(pipe, X, y, cv=5).mean()
print(f"leaky estimate: {leaky:.3f}")
print(f"honest estimate: {honest:.3f}")leaky estimate: 0.830
honest estimate: 0.500
The leaky procedure reports 83 % accuracy on random noise. It picked the 20 features that happened to correlate with the labels of all 100 rows, including the rows later used for testing. Putting every data-dependent step inside the pipeline, so it is refitted on each training fold, gives the honest 50 %. The Data Preparation Toolkit course lists the leakage rules in full.
5. Choose preparation methods based on the data
Tabular, image, text, audio, video and time-series data each need their own pipeline: scaling and encoding for tables, resizing and augmentation for images, tokenisation for text, spectrograms for audio, time-aware splits for series. There is no universal preprocessing recipe.
6. Keep experiments reproducible
Record the data version, the code version, the configuration, the random seeds, the metrics and the saved model for every experiment. Then any result can be rerun, compared and explained. The batch learning lesson showed that a fixed seed reproduces a model exactly.
7. Evaluate on genuinely unseen data
Keep a test set that is used once, at the end. Every time you look at test results and change something, the test set leaks a little into your decisions, and its score becomes optimistic. Tune on a validation set or with cross-validation instead.
8. Monitor deployed systems
Data distributions and the real world change after deployment. Watch inputs, outputs and, when labels arrive, accuracy; alert on drift; plan retraining. The lifecycle lesson showed a silent drop from 0.958 to 0.643 that only monitoring would catch.
9. Learn the mechanics by implementing them
Build small versions yourself, as this course did: kNN, k-means, gradient descent, a two-layer network, Q-learning, self-training. Once you have written a training loop by hand, library calls stop being magic, and you can debug them when they misbehave.
10. Treat ML as a complete engineering lifecycle
Data collection, preparation, training, evaluation, deployment and monitoring are all essential. A model is one component of a system; most of the work, and most of the failures, are in the rest.
A checklist for your projects
| Before training | During development | Before and after release |
|---|---|---|
| problem, metric and constraints written down | baseline and simple model first | test set used once |
| data inspected; labels checked | all preprocessing inside the pipeline | model, data and code versions recorded |
| splits made before any fitting | experiments logged with seeds and settings | monitoring and a retraining plan in place |
Practice
- A colleague reports 99 % accuracy for a new churn model. What two numbers do you ask for first?
Answer
The baseline accuracy (what does "never churns" score? if only 1 % churn, it already scores 99 %) and a metric for the rare class, such as recall or precision for churners. Also ask how the 99 % was measured: on which data and after which preprocessing.
- In the leakage example, change
k=20tok=200in both places. Do you expect the leaky estimate to go up or down? Run it to check.
Answer
It goes up, to 0.980, while the honest estimate stays near chance (0.550). With 200 features chosen for their chance correlation with all 100 labels, the leaky model has even more "evidence" that is really just the test labels leaking in, and with more features than rows a linear model can separate almost anything. More leakage means a more convincing lie. Only the honest procedure tells the truth.
Test yourself
On the breast-cancer data, always predicting the majority class scored 0.626. What role does that number play?
- It is the final model's score
- It is the floor any useful model must clearly beat
- It is a sign of leakage
- It is the maximum possible accuracy
Why did the leaky procedure report 0.830 on pure noise?
- Logistic regression is biased
- Features were selected using all rows, including those later used as test folds
- Cross-validation is broken
- The seed was wrong
Which practice keeps the test set honest?
- Checking the test score after every change
- Tuning hyperparameters on the test set
- Using the test set once, at the end, and tuning on validation data
- Making the test set smaller
What should be recorded to keep an experiment reproducible?
- Only the final accuracy
- Data version, code version, configuration, seeds, metrics and the saved model
- The name of the person who ran it
- Nothing, if the code is on GitHub