Sub topic 2
A Repository Layout That Scales
Assume you know basic Python modules and functions, can use the command line, and have used Git. You should also have trained at least one model with a library such as scikit-learn or PyTorch. This chapter focuses on where project files belong, not on a particular machine learning framework.
A machine learning repository should make each kind of change easy to find. Training code, configuration, data, experiments, and tests have different lifecycles. Keeping them in separate locations reduces accidental changes and makes a run easier to repeat.
A practical starting layout
my_project/
├── README.md
├── pyproject.toml
├── configs/
│ └── train.yaml
├── data/
│ ├── raw/
│ ├── interim/
│ └── processed/
├── models/
├── notebooks/
├── src/
│ └── my_project/
│ ├── __init__.py
│ ├── config.py
│ ├── data/
│ │ └── prepare.py
│ └── train.py
└── tests/
└── test_config.pyThe src/ directory contains an importable Python package. The package name, my_project, should be different from the repository name only when that makes sense for your project. Put reusable data preparation, feature engineering, training, and evaluation functions here.
The configs/ directory contains values that may change between runs, such as a data path, learning rate, batch size, or random seed. Keeping these values outside Python files lets you compare runs without editing the training logic.
The data/ directory separates source data from derived data. Raw data should remain unchanged. Interim data contains temporary or partially transformed results. Processed data is ready for training or evaluation. Large data files usually do not belong in ordinary Git history.
The tests/ directory contains automated checks for functions and data transformations. Tests help detect quiet errors, such as a changed column name or a configuration value loaded as the wrong type.
The notebooks/ directory is useful for exploration, visualization, and explaining results. A notebook can call functions from src/my_project rather than containing the only copy of important logic.
| Location | Main purpose | Typical contents |
|---|---|---|
| `src/my_project/` | Reusable application code | Preparation and training modules |
| `configs/` | Run-specific settings | YAML configuration files |
| `data/` | Data at processing stages | Raw and derived datasets |
| `tests/` | Repeatable checks | Unit and data validation tests |
| `notebooks/` | Exploration and reports | Analysis notebooks |
| `models/` | Generated model files | Versioned or temporary artifacts |
How the pieces connect
A typical run reads a configuration, loads a data version, imports preparation code, trains a model, and writes an output artifact. The notebook may visualize the output, but the package contains the logic that should work from the command line or in automation.
Common layout mistakes
- Putting all code in one notebook makes reuse and review difficult.
- Mixing raw and processed data makes it unclear which input produced a result.
- Saving secrets or local paths in tracked configuration files can expose credentials or break another developer’s run.
- Naming every output
model.pklcauses later runs to overwrite earlier results. - Importing files through fragile relative paths often works only from one working directory.
Start with a small layout and add directories when a real need appears. The goal is a clear boundary between reusable code, changeable settings, data, and generated results.
Exercise
Where should a reusable feature-engineering function live, and where should an exploratory chart live?
Show answer
Put the function in the importable package under src/my_project/, and put the exploratory chart in a notebook under notebooks/.