Learn / ML & Data Foundations / Module 01

ML & Data Foundations · Module 01

Exploratory Data Analysis, Clean Splits, and Data Leakage

Build the habit of inspecting raw data before modeling and splitting train, validation, and test sets without leaking future or group information.

12 min Foundations Updated 2026-12-03 EDAData SplitsLeakagescikit-learn
Silent visual walkthrough. Read the lesson.

<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->

Inspect the data before importing a model

Most machine learning projects go sideways in the first 30 minutes—not because someone picked the wrong neural architecture, but because nobody checked the table they loaded.

Run a five-point EDA checklist on every new dataset:

  1. Shape and grain: what does one row represent (one customer, one transaction, one 1-minute sensor reading)?
  2. Target balance: how common is each class or outcome?
  3. Missingness: are nulls random, or do they signal an event (such as a sensor outage)?
  4. Impossible values: negative power, timestamps in 1970, duplicate primary keys, or mixed units (kW vs. W).
  5. Identifiers and timestamps: which columns identify a site, patient, or date? You need those to split honestly.

Why random splits often lie

train_test_split(..., shuffle=True) is only honest when rows are completely independent. In real engineering and business datasets, they rarely are:

Data structureWhat goes wrong with a random splitHonest split strategy
Time series (sensor logs, sales, weather)Future readings leak into training and inflate accuracy.Chronological split (train on earlier dates, test on later dates).
Grouped entities (multiple rows per site, user, or patient)Same entity appears in train and test; model memorizes the entity.GroupKFold / group split (hold out whole sites or users).
Rare classes (0.2% fraud or faults)A random split may leave almost zero positives in test.Stratified split (preserving class ratio in each fold).

A leak-free baseline template in scikit-learn

Putting preprocessing and modeling inside one Pipeline guarantees you never accidentally fit on test data:

python
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42
)

pre = ColumnTransformer([
    ("num", Pipeline([
        ("impute", SimpleImputer(strategy="median")),
        ("scale", StandardScaler()),
    ]), num_cols),
    ("cat", Pipeline([
        ("impute", SimpleImputer(strategy="most_frequent")),
        ("encode", OneHotEncoder(handle_unknown="ignore")),
    ]), cat_cols),
])

baseline = Pipeline([
    ("pre", pre),
    ("clf", LogisticRegression(max_iter=1000, class_weight="balanced")),
])

baseline.fit(X_train, y_train)

<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->

Finished this module?

Mark it complete to update your browser-local progress bar for ML & Data Foundations.