← Back to Curriculum
The ML Workshop·30 min·Beginner
🧹

Data Preprocessing

Cleaning, scaling, encoding, and splitting

Before any model can learn, the data needs a thorough clean-up. Here we learn how to wash, organize, and portion our data like a chef prepping ingredients.

Before We Begin

What we are learning today

Garbage in, garbage out—you have probably heard that phrase, and it is the single most important rule in machine learning. A model is only as good as the data it trains on. In this lesson we walk through every step of getting raw data ready: handling missing values, scaling numbers so they play nicely together, encoding categories into numbers a model can read, and splitting everything into training, validation, and test sets so we can measure real progress without fooling ourselves.

How this lesson fits

Theory is only half the story. In this module we roll up our sleeves and learn the craft behind every successful ML project: preparing data, engineering features, and strengthening our statistical intuition. These are the skills that separate a notebook experiment from a model you can actually trust.

The big question

What does raw, messy, real-world data need before a model can learn anything useful from it?

Clean, encode, and scale a dataset so a model can digest itSplit data into training, validation, and test sets—and explain why each mattersApply probability and hypothesis testing to make data-driven decisions

Why You Should Care

In the real world, data arrives messy: missing entries, inconsistent formats, wildly different scales. Students who master preprocessing will save hours of debugging and build models that actually generalize. This is the single most time-consuming step in any ML project, and the one professionals say matters most.

Where this is used today

  • Hospital records: filling missing patient readings before diagnosis models
  • E-commerce: normalizing prices across currencies for recommendation engines
  • Self-driving cars: cleaning and synchronizing sensor streams before training

Think of it like this

Think of a restaurant kitchen before dinner service. The chef doesn't start cooking the moment ingredients arrive. First, vegetables are washed, proteins are trimmed, sauces are measured, and stations are organized. Only then does cooking begin. Preprocessing is that mise en place for your model—skip it, and the dish falls apart.

Easy mistake to make

Scaling and encoding are not optional extras—they are critical. A model that receives unscaled features may weight them unfairly, and raw text categories will crash most algorithms entirely.

By the end, you should be able to say:

  • Identify and handle missing values using imputation or removal
  • Explain when and why to apply min-max scaling vs. standardization
  • Convert categorical features with one-hot or label encoding
  • Split a dataset into train / validation / test and justify the ratios

Think about this first

Imagine you get a spreadsheet where some cells say "N/A," ages range from 0 to 200, and one column mixes "Yes," "yes," and "1." What would you fix first, and why?

Words we will keep using

imputationnormalizationstandardizationone-hot encodingtrain-test splitdata leakage

Garbage In, Garbage Out

A model is only as good as its data. Real-world datasets arrive with missing values, impossible outliers, inconsistent formats, and wildly different scales. Preprocessing is the kitchen prep before cooking \u2014 skip it and the recipe falls apart.

ImputeFill missing or impossible values with column statistics (mean, median, mode).
ScaleNormalize numeric features so no single column dominates by sheer magnitude.
EncodeConvert text categories into numbers a model can process.

Interactive: Clean This Dataset

This dataset has impossible ages (0, 200), missing incomes (0), null scores, and a text column. Apply preprocessing steps in order and watch the data transform.

Raw Data

ageincomecityscore
25$45,000NYC72
0$52,000LA85
38N/ANYC90
47$120,000Chicagonull
200$38,000LA65
31$75,000Chicago78
29N/AHouston82
55$95,000NYCnull
42$68,000Houston70
33$58,000LA88

After Processing

ageincomecityscore
25$45,000NYC72
0$52,000LA85
38$0NYC90
47$120,000Chicago
200$38,000LA65
31$75,000Chicago78
29$0Houston82
55$95,000NYC
42$68,000Houston70
33$58,000LA88

Why Scaling Matters

Income ranges from $38,000 to $120,000 while age ranges from 25 to 55. Without scaling, a distance-based model would treat income as 1000x more important than age simply because the numbers are bigger.

Min-max scaling squashes every column into [0,1][0, 1] so the model treats all features equally.

Train / Validation / Test Split

Never test on what you trained on. The golden rule: split your data into three buckets.

Training (60%)The model sees this data and learns from it.
Validation (20%)Used to tune hyperparameters and pick the best model.
Test (20%)Locked away until the very end. The final, honest score.
Data leakage happens when information from the test set accidentally influences training \u2014 like normalizing using statistics computed on the full dataset instead of just the training split. Always fit your preprocessing on training data only, then transform the rest.