AI from scratch

Module 06 · Data & Feature Engineering

Messy data beats clever models

Pipelines, leakage-safe splits, features, imbalance, and labels — the unsexy work that decides whether your model survives contact with reality.

The data pipeline

sources → extract → validate → transform/features
       → split → train/eval → store artifacts → monitor inputs in prod

Industry habit: fit transforms on train only, then apply to val/test/prod with the same statistics. Scalers, imputers, target encoders — all leak if fitted on the full dataset before the split.

Validate early: null rates, ranges, sudden schema changes, duplicate keys. A model can’t fix a broken join.

Leakage & honest splits

Leakage = information unavailable at prediction time helped training. Metrics look heroic; production falls over.

Split recipes: random (i.i.d.), time-based (train past / test future), grouped (keep entities together), stratified (preserve class rates — critical for fraud).

FraudWatch reminder: drop or handle Time carefully, stratify rare fraud labels, and never tune on the final test set. See projects start guide.

Feature engineering that matters

Tree models often need less scaling; linear models and neural nets care more. Always ask: would this feature exist in the live request path?

Feature importance & explainability

Tree gain/permutation importance and SHAP-style attributions help debug and satisfy “why?” questions in regulated domains. Keep a glass-box baseline even if a black box scores slightly higher.

Imbalance & rare events

Fraud, defaults, failures — rare positives. Recipe:

  1. Pick the right metric (PR-AUC, precision@k, recall at fixed precision)
  2. Class weights / calibrated probabilities
  3. Threshold for the business cost matrix
  4. Only then consider resampling tricks

Don’t jump to SMOTE before a weighted baseline and a clear cost story — interviewers notice.

Labels are a product

Garbage labels → garbage models. Budget for:

Data-centric AI in one sentence: improving labels and slices often beats another model architecture.

Data for GenAI / RAG

Interview questions

Also in the interview bank.

What is data leakage? Give an example.

Training with information you wouldn’t have at prediction time — e.g. fitting a scaler on train+test, or a post-outcome feature. Split first; fit on train only.

Random split or time-based split?

Time-based when the world drifts (fraud, demand, news). Random only when i.i.d. is a fair assumption.

How do you handle class imbalance?

Correct metrics first, then class weights / thresholds / more minority data. Resampling is optional, not step one.

What makes a good feature?

Available at prediction time, stable, predictive, and explainable enough for the domain — validated without leaking the future.