Data Versioning and Validation
Git for code, DVC for large data, and validation schemas that stop corrupt data from reaching the model.
DVC in brief
pip install dvc
git init && dvc init
dvc add data/raw/images.zip # creates data/raw/images.zip.dvc
git add data/raw/images.zip.dvc data/raw/.gitignore
git commit -m "Add raw images v1"
dvc remote add -d storage /path/to/remote/storage
dvc pushTo go back to a previous version: git checkout <commit> then dvc checkout.
Validation with a schema
import pandas as pd
import pandera.pandas as pa # in older versions: import pandera as pa
schema = pa.DataFrameSchema({
"age": pa.Column(int, pa.Check.in_range(0, 120)),
"sex": pa.Column(str, pa.Check.isin(["F", "M"])),
"glucose": pa.Column(float, pa.Check.ge(0), nullable=True),
"label": pa.Column(int, pa.Check.isin([0, 1]), nullable=False),
})
df = pd.read_csv("patients.csv")
schema.validate(df, lazy=True) # collects all violations, then raises an exceptionWhat must be checked
| Category | Examples |
|---|---|
| The architecture | The columns and types exist |
| Values | The range, and the allowed categories |
| Completeness | The proportion missing is within acceptable limits |
| Distribution | The mean and variance are close to the reference |
| Repetition | No duplicate rows, and no overlap between training and test |
Documenting data
A short datasheet for each dataset: who collected it? When? How was it labelled? Which groups are under-represented? What uses are inappropriate?
Lesson terms
- The data version
- Saving specific, retrievable snapshots of datasets and linking them to the experiments that used them.
- Data validation
- Automated checking of data against explicit expectations about its structure and values before it is used.
- Datasheet
- A document describing the motivation for creating the dataset, its composition, how it was collected, its recommended uses and its limits.
Abstract
Data changes: corrected labels, added batches, or a modified source. Data versioning keeps a retrievable snapshot of every dataset that was used. DVC stores large data outside Git and keeps small files in Git that point to it by hash. Data validation enforces an expected schema (types, ranges, missing values and categories) and fails early on violations. Documenting data in datasheets records its source and limitations.
Review questions
-
Why don't we put large data files directly in Git?
Because Git is not designed for large binary files, so the repository grows and slows down, while DVC stores them in an external store with pointers in Git.
-
What is an example of a useful validation rule?
Age between 0 and 120, no missing values in the label column, and categories within a known set.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate