Sub topic 1
Why structure matters: reproducible results
Prerequisites
This chapter assumes you can write basic Python modules and functions, work comfortably at the command line, use Git to commit, branch, and ignore files, and have trained at least one model with a library such as scikit-learn or PyTorch.
Why structure matters
A result is reproducible when you or a colleague can rerun the project and get the same numbers. Projects often fail this test for simple reasons: code lives in a notebook cell that was edited out of order, a learning rate is typed into a function, and a data file was overwritten. A clear layout gives each piece a known place, so each piece can change without breaking the others.
A repository layout that scales
Give each folder one job:
my_project/
├── configs/ # run settings, tracked in Git
├── data/ # raw, interim, processed (not in Git)
├── notebooks/ # exploration only
├── src/my_project/ # importable, reusable code
└── tests/Moving reusable code into a package
Logic that more than one step needs belongs in src/my_project/, not in a notebook. A package can be imported, tested, and run from the command line:
from my_project.features import make_features
X = make_features(raw_df)Install the package in editable mode with pip install -e . so the same import works in notebooks and scripts.
Where notebooks fit
Notebooks suit exploring data and plotting results. They are weak for repeatable runs because cells can run out of order and leave hidden state behind. When a notebook cell becomes something you need twice, move it into the package and import it back.
Keeping configuration out of code
Hard-coded values make runs hard to compare. Store them in a YAML file:
learning_rate: 0.001
batch_size: 32
seed: 42Then read the file instead of typing the numbers:
import yaml
with open("configs/train.yaml") as f:
cfg = yaml.safe_load(f)
print(cfg["learning_rate"])Each run now reads its settings from one tracked file. Changing a setting means editing one line, and the diff in Git shows exactly what changed.
Exercise
Exercise
A teammate changes the learning rate inside the training function and does not record it anywhere. Why does this hurt reproducibility, and where should the value live instead?
Show answer
The value is no longer visible in a tracked file, so no one can tell which rate produced a result. It should live in a YAML file under configs/, which Git records with each commit.