Project Structuring & Cookiecutter Data Science
Organize repos with clear data/src/models layouts for collaboration and reproducibility.
In plain words
Project structure means keeping your files in sensible, predictable places so that anyone, including future you, can find things, run them again and get the same result.
Think of it like…
A well-run kitchen. Ingredients arrive in labelled storage that nobody alters (raw data), prep stations have fixed places (data, features, models), the recipe book is versioned (code) and the line cook can reproduce any dish from the card without asking the chef who invented it.
Where you’ve seen it
A churn project starts as one 1,200-line notebook. The same cleaning function is copy-pasted into five notebooks (about 40 lines each, 200 lines of duplication); when a bug in handling blank TotalCharges is fixed in one, four notebooks keep the bug. The team restructures: data/raw/telco.csv is read-only and git-ignored (a 2 GB file in Git would slow every clone and can't be diffed), src/data/make_dataset.py cleans it and performs the train/validation/test split first and saves the splits to data/processed/, src/features/build_features.py holds a scikit-learn ColumnTransformer fitted on training data only, src/models/train.py and predict.py read parameters from params.yaml, and notebooks/01-hk-eda.ipynb follows a numbered naming convention (order, initials, topic) so exploration stays chronological. A Makefile defines make data, make features, make train, make evaluate and make test, so a newcomer reproduces the model with four commands. Dependencies are pinned (pip-compile or a lock file) and python-dotenv reads secrets from an untracked .env with a checked-in .env.example. tests/ includes a unit test for clean_total_charges and a data test that fails if the raw file has unexpected columns, and a pre-commit hook strips notebook outputs (a notebook with embedded charts can add megabytes to every commit) and runs a formatter. A fixed random seed lives in one config location. Onboarding a new analyst drops from two days of questions to an afternoon: clone, create the environment, run the four commands, compare the metrics file with the committed one. Because the pipeline is code, the same src/ package is later imported by the API and by scheduled retraining, with no copy-paste from notebooks.
Words you’ll hear
- Immutable raw data
- Original data that is never edited; all cleaning happens in code that writes new files.
- Package (src/)
- Reusable, tested code imported by notebooks, pipelines and services.
- Makefile target
- A named command, such as make train, that reproduces a pipeline step.
- Environment pinning
- Fixing exact dependency versions so results can be recreated.
Visual diagram
Theory & deep dive
Layout and the principles behind it
A typical layout: data/raw (original, untouched), data/interim (intermediate), data/processed (model-ready), data/external (third-party), notebooks/ (numbered, with author and topic), src/ (data/, features/, models/, visualization/), models/ (serialised artefacts), reports/ and reports/figures/, tests/, docs/, plus README.md, Makefile, a dependency file and .gitignore. The principles: raw data is immutable and every transformation is code, so you can always regenerate downstream files; notebooks are for exploration and anything reused moves into src/ as a function with a test; pipelines are commands (a Makefile, DVC stages or a workflow tool) rather than a sequence of notebook cells run by hand; and configuration is separate from code (params.yaml, .env), so experiments change a file, not the source.
Structure also prevents statistical mistakes. Making the train/validation/test split the first step in make_dataset.py, and fitting imputers and encoders only on the training split inside a pipeline object, removes the most common source of leakage. Keeping seeds in one config, pinning library versions and recording the git commit with each result makes numbers reproducible. Small habits help: name notebooks 01-hk-eda.ipynb so order and owner are clear, keep functions short and pure, add type hints and docstrings to src/ code and review notebooks by diffing exported scripts or clearing outputs first.
Five notebooks each contain a 40-line cleaning function (200 lines of duplication) with the same bug. Move it to src/data/clean.py::clean_total_charges, add a test with blank, numeric and negative values, and import it in all five: one fix, one test, 160 fewer lines to maintain.
Reproducibility, collaboration and what stays out of Git
What goes in Git: code, configs, notebooks (outputs stripped), small reference files and documentation. What stays out: large datasets and models (track them with DVC or object storage, see the next lessons), secrets (.env, API keys: commit .env.example instead) and generated files. A 2 GB CSV in a repository bloats every clone and can never be fully removed from history. Add a .gitignore for data/, models/ and environment folders on day one, and use nbstripout or a pre-commit hook to remove notebook outputs, which otherwise add megabytes of base64 images to each commit.
Reproducibility checklist: pinned dependencies (lock file), recorded Python version, fixed seeds, versioned data, one command to run the pipeline, committed metrics (reports/metrics.json) so a reviewer sees changes in a pull request, and a README that states the problem, data source, how to run, results and known limitations. For collaboration, add linting and formatting (ruff, black), tests for the functions in src/ and a CI job that runs make test and a small smoke training on a data sample. As projects grow, the same package is imported by the prediction API and the retraining pipeline, so keep src/ free of notebook-only code and avoid hard-coded paths (use a config or pathlib relative to the project root).
git clone … → make environment → make data (download, clean, split) → make features → make train → make evaluate. Output: reports/metrics.json with AUC and PR-AUC; compare with the committed version. A newcomer should reproduce the baseline within an afternoon.
How it works, step by step
Step 1 of 41. Create the skeleton
Start from a template with data, notebooks, src, models, reports and tests, and add a .gitignore for data and secrets.
- Pin
- dependencies
When to use it & trade-offs
What it is
A project structure is the agreed layout of folders and files that separates data, exploration, reusable code, models, reports and configuration. The widely copied Cookiecutter Data Science template (from DrivenData) gives a starting point: data/ with raw, interim, processed and external, notebooks/ for exploration, a Python package under src/ split into data, features and models, models/ for trained artefacts, reports/ for outputs, tests/, a Makefile with commands such as make data and make train, a pinned dependency file and a README. The principles matter more than the exact folders: raw data is immutable, notebooks explore, and code in src/ runs in production, every step is a command anyone can re-run, and environments and randomness are pinned.
Why it matters: Most data science work is read, rerun and handed over: by a teammate next month, by a reviewer, by a production pipeline or by you after six weeks away. A project that lives in a pile of notebooks named final_v3_REAL.ipynb can't be tested, reproduced or deployed, and bugs are fixed in one copy of a duplicated function but not the other four. A consistent layout lowers onboarding time, makes code review possible, keeps large data out of Git and turns experiments into something that can ship.
When to reach for it
- Any model that will be retrained or deployed
- Team projects needing review and CI
- Portfolio projects that must be reproducible
When to avoid or be careful
- Editing raw data files in place
- Committing large datasets, models or secrets
- Leaving core logic only inside notebooks
Ways to organise work
Strengths: Reproducible, testable, ready for CI.
Watch out: Some upfront structure.
Strengths: Fast to start; great for visuals.
Watch out: Hard to test, diff and reuse.
Strengths: Dependency tracking; reproducible runs.
Watch out: More concepts to learn.
Interview signals & numbers to know
When this topic comes up
- “How do you structure a data science repository?”
- Notebooks versus production code
- Reproducibility checklist
- What should not be in Git
- Preventing leakage through structure
Benchmarks & rules of thumb
- 4 commands
- data, features, train, evaluate to reproduce a model
- 200 lines → 1 function
- duplication removed in the example refactor
- 2 GB
- data file that should never enter Git history
Common mistakes & follow-ups
Common interview mistakes
- Fitting scalers before splitting the data.
- Hard-coding absolute paths from one laptop.
- Committing notebooks with huge embedded outputs.
Likely interviewer follow-ups
- How would you hand this project to a teammate?
- How do you version data that doesn't fit in Git?
- What tests would you write for a data science package?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Project Structuring & Cookiecutter Data Science with data science interview Q&A and hands-on practice.