Data preprocessing is the set of steps that turn raw, messy real-world data into a clean numeric table a model can actually learn from. In most ML projects, this takes longer than training the model itself.
The Standard Preprocessing Sequence
| Step | What It Solves | Note |
|---|---|---|
| 1. Clean | Wrong dtypes, inconsistent text, structural errors | Data Cleaning |
| 2. Handle duplicates | Repeated rows that bias training | Duplicate Data |
| 3. Handle missing values | Gaps in the data most algorithms can't accept | Missing Values |
| 4. Handle outliers | Extreme values that distort statistics and distance-based models | Outlier Treatment |
| 5. Encode categories | Text categories most algorithms can't accept | One-Hot Encoding |
| 6. Scale numeric features | Features on wildly different scales dominating distance/gradient calculations | Feature Scaling |
| 7. Split before any of the above touches test data | Preventing data leakage | Data Leakage |
The order matters less than one rule: split your data into train/test first, then fit every preprocessing step (imputer, encoder, scaler) only on the training set, applying the same learned transformation to the test set. See Preprocessing Pipeline.
Why Models Need This At All
- Most algorithms require purely numeric input — text categories and missing cells simply can't be processed as-is
- Distance-based and gradient-based algorithms (KNN, SVM, linear/logistic regression, neural nets) are sensitive to feature scale
- Garbage in, garbage out: a model trained on inconsistent or leaked data produces unreliable predictions no matter how sophisticated the algorithm
Common Mistakes
- Jumping to model training before checking
df.info()anddf.isnull().sum()— see Pandas for ML. - Fitting a scaler, encoder or imputer on the full dataset before splitting — the single most common source of data leakage.
- Treating preprocessing as a one-time, throwaway script instead of a reusable pipeline that must run identically at prediction time in production.
Interview Relevance
Q: "Walk me through how you'd preprocess a raw dataset before modeling." Clean → dedupe → handle missing values → handle outliers → encode categoricals → scale numerics — with train/test split happening first, and every fitted step (imputer, encoder, scaler) learned only from the training set.
Practice Question
You're given a raw CSV with missing values, duplicate rows, a "city" text column and features on very different numeric scales. List the preprocessing steps you'd apply, in order.