Sub topic 1
Why Structure Matters: Reproducible Results
Prerequisites
This chapter assumes basic Python, modules, and functions; comfort with the command line; basic Git usage; and experience training at least one model with scikit-learn or PyTorch.
A machine learning result is reproducible when another person, or your future self, can recreate it from the recorded code, data, configuration, and environment. Reproducibility does not mean every run always produces identical bits. Some hardware and libraries introduce small differences. It means the inputs and decisions are known well enough to explain and repeat the result.
A model depends on more than its training function. It also depends on the data version, feature-processing code, hyperparameters, random seeds, package versions, and the command used to start the run. If these details are mixed together or left in a notebook cell, a successful result becomes difficult to inspect.
The problem with an unstructured project
Imagine a notebook that:
- Downloads data directly from a changing URL.
- Removes missing values in one cell.
- Defines a learning rate in another cell.
- Trains a model after several cells of hidden state.
- Saves the model as
final.pkl.
This workflow may work once. It is hard to answer basic questions later:
- Which data produced
final.pkl? - Which learning rate was used?
- Did the preprocessing code change?
- Were cells run in the intended order?
- Can someone else run the same process without your notebook session?
A project becomes easier to reproduce when each input has a clear location and role.
A structure for traceable runs
Keep reusable code in a source package, changing values in configuration files, and data in explicitly named stages. Track small text files in Git, but store large datasets and models in a suitable artifact or data store.
A useful repository separates concerns:
| Concern | Typical location | What it contains |
|---|---|---|
| Reusable code | `src/my_project/` | Loading, preprocessing, training |
| Run values | `configs/` | Paths, seeds, and hyperparameters |
| Data stages | `data/raw/`, `data/processed/` | Original and derived data |
| Verification | `tests/` | Checks for code and data assumptions |
| Exploration | `notebooks/` | Visual analysis and experiments |
The separation lets each part change independently. You can try a new learning rate without editing training logic. You can update preprocessing while keeping the command-line interface stable. You can inspect a notebook without treating it as the only source of production code.
A minimal reproducibility record
At the end of a run, record the values that identify it. A small JSON file is often enough to begin:
import json
from pathlib import Path
run = {
"code_version": "git-commit-abc123",
"data_version": "orders-2026-02-01",
"learning_rate": 0.001,
"seed": 42,
"validation_accuracy": 0.91,
}
Path("artifacts/run.json").parent.mkdir(exist_ok=True)
Path("artifacts/run.json").write_text(json.dumps(run, indent=2) + "\n")The exact tracking tool can vary. The important practice is to record parameters, metrics, input versions, and the resulting artifact together.
Exercise
Why is a notebook-only workflow difficult to reproduce?
Show answer
Its execution order, hidden state, hard-coded values, data source, and environment may not be recorded clearly enough for another run.