Sub topic 4
Managing Data: Collection, Validation and Versioning
Assume you know basic Python, have trained a supervised model, and understand Git and command-line tools. This chapter focuses on the data layer that makes later training and deployment trustworthy.
Why data management matters
A model learns from the examples it receives. If those examples are incomplete, incorrectly labeled, duplicated, or different from the data available in production, a strong algorithm can still produce a weak system.
Data management creates traceable artifacts:
- Raw data: the original records, preserved without editing.
- Validated data: records that pass schema and quality checks.
- Dataset version: an immutable snapshot identified by a version or content hash.
- Data-quality report: measurements such as row count, missing values, duplicate count, and label balance.
- Lineage record: information about where the data came from and which code created each derived dataset.
Keep raw data separate from cleaned data. Cleaning should create a new artifact, not silently overwrite the source.
Collecting data with a contract
A data contract describes what a producer promises to provide. It normally specifies:
- Required columns and their types.
- Allowed ranges and categories.
- Whether a value may be missing.
- The event time and entity identifier.
- Rules for deduplication and late-arriving records.
- Privacy, retention, and access requirements.
For supervised learning, also record how labels were created. A label may arrive later than its features, so the collection process must prevent future information from leaking into training examples.
Validating a dataset
Validation should run before feature engineering and training. Separate checks into two groups:
| Check type | Example | Failure response |
|---|---|---|
| Schema | `age` is numeric and `customer_id` exists | Reject the file |
| Quality | Missing rate is below 2% | Quarantine or investigate |
A useful validator fails loudly on structural errors and reports statistical warnings for review.
import csv
import hashlib
from pathlib import Path
rows = [
{"customer_id": "c1", "age": "34", "label": "1"},
{"customer_id": "c2", "age": "51", "label": "0"},
]
path = Path("customers.csv")
with path.open("w", newline="") as file:
writer = csv.DictWriter(file, fieldnames=rows[0].keys())
writer.writeheader()
writer.writerows(rows)
required = {"customer_id", "age", "label"}
with path.open(newline="") as file:
reader = csv.DictReader(file)
if set(reader.fieldnames or []) != required:
raise ValueError("Unexpected schema")
records = list(reader)
if any(not row["customer_id"] for row in records):
raise ValueError("Missing customer ID")
if any(int(row["age"]) < 0 for row in records):
raise ValueError("Age cannot be negative")
digest = hashlib.sha256(path.read_bytes()).hexdigest()
print(f"validated_rows={len(records)}")
print(f"dataset_hash={digest}")Versioning and reproducibility
A dataset version should identify the exact bytes or logical snapshot used for training. Store its hash, source location, collection time, schema version, validation results, and sampling rules. A Git commit alone is not enough because the same code can produce different results from changed data.
A practical workflow
- Land incoming data in an access-controlled raw area.
- Assign a source and ingestion timestamp.
- Run schema, quality, privacy, and leakage checks.
- Save a validated snapshot with a unique version.
- Record the snapshot in the training manifest.
- Retain rejected records and validation reports for investigation.
- Compare new quality reports with previous versions.
Exercise
A new dataset has the expected columns, but its duplicate customer rate rises from 0.1% to 18%. Should training continue automatically?
Show answer
No. Quarantine the snapshot and investigate the upstream collection or deduplication process before training.