Sub topic 8
Organizing Raw, Interim, and Processed Data
Assume you know basic Python, command-line work, Git, and model training. This chapter focuses on a data layout that makes a machine learning project easier to reproduce and debug.
Why separate data by stage?
Training code usually does not consume the original files directly. Data passes through several transformations:
- Raw data is the original copy received from a source. Keep it unchanged.
- Interim data is data after temporary cleaning or joining steps. It may still need more preparation.
- Processed data is data in the stable format used for training, validation, or testing.
A useful layout is:
project/
├── data/
│ ├── raw/
│ │ ├── customers.csv
│ │ └── transactions.json
│ ├── interim/
│ │ └── joined_transactions.parquet
│ └── processed/
│ ├── train.parquet
│ ├── validation.parquet
│ └── test.parquet
├── src/
│ └── project_name/
│ └── data/
│ ├── download.py
│ ├── clean.py
│ └── split.py
└── configs/
└── data.yamlThe exact file types do not matter as much as the boundaries. A later step should not silently overwrite an earlier step's input.
A data flow with clear boundaries
Raw data is an audit point. If a cleaning rule later turns out to be wrong, you can rerun the process from the original input. Do not edit raw files by hand.
Interim data is useful when a transformation is expensive or when a stage has a meaningful output. You do not need a folder for every small operation. Add a stage when its output helps debugging, reuse, or review.
Processed data should have a documented schema. A schema describes columns, data types, allowed values, and the meaning of each field. Record how the data was split and which rows belong to each set.
Keep data out of ordinary Git history
Large datasets and generated files usually do not belong in Git. Add them to .gitignore:
data/raw/
data/interim/
data/processed/
models/Ignoring a path does not version the data. Use object storage or a data-versioning tool such as DVC when you need to identify the exact dataset used by a run. Store a small pointer or metadata file in Git, including the source identifier, checksum, creation time, schema version, and transformation code version.
A checksum is a short value calculated from file contents. If the contents change, the checksum normally changes too. This gives you a practical way to detect accidental replacement.
Validate paths and stages
Make stage boundaries explicit in code:
from pathlib import Path
DATA_DIR = Path("data")
RAW_DIR = DATA_DIR / "raw"
INTERIM_DIR = DATA_DIR / "interim"
PROCESSED_DIR = DATA_DIR / "processed"
def require_input(path: Path) -> None:
if not path.exists():
raise FileNotFoundError(f"Missing pipeline input: {path}")
def describe_layout() -> None:
for name, path in (
("raw", RAW_DIR),
("interim", INTERIM_DIR),
("processed", PROCESSED_DIR),
):
print(f"{name}: {path}")
if __name__ == "__main__":
require_input(RAW_DIR / "customers.csv")
describe_layout()A pipeline tool can then declare that a preparation stage reads data/raw and writes data/interim, while a training stage reads data/processed. This makes dependencies visible and allows unchanged stages to be skipped.
Exercise
Why should a cleaning script read from data/raw and write to data/interim instead of modifying the raw file?
Show answer
Keeping raw data unchanged preserves an audit point and lets you rerun or correct the cleaning process from the original input.