Master data processing, modeling, and deep learning algorithms.
Data preprocessing is the step between raw data and a working machine-learning model: it transforms messy, incomplete, inconsistent records into clean, consistent input. A model learns exactly the patterns in the data you give it, so bad or unprocessed data produces bad results no matter how clever the algorithm is. This lesson walks through the whole pipeline: handling missing values, removing duplicates, treating outliers, encoding categorical variables, scaling features, splitting data, and avoiding the subtle trap of data leakage. You will also see feature engineering and how to handle imbalanced data. The running example is a small, messy customer dataset that you clean step by step.
| Raw data | Prepared data | |
|---|---|---|
| Missing values | Present | Handled (imputed or dropped) |
| Types | Mixed, inconsistent | Consistent, numeric where needed |
| Scale | Different ranges | Comparable (scaled) |
| Categorical | Text labels | Encoded numerically |
Most real datasets arrive in a form a model cannot consume directly. Preprocessing is the translation layer.
| Approach | What it does | When to use |
|---|---|---|
| Drop rows | Remove records with missing values | When few rows are missing and data is plentiful |
| Drop columns | Remove a mostly-empty column | When a column is almost entirely missing |
| Impute | Fill in a sensible value | When you cannot afford to lose the data |
| Type | Example | Typical treatment |
|---|---|---|
| Numerical | Age = 34 | Scale to a common range |
| Categorical (nominal) | City = "Accra" | One-hot or label encode |
| Categorical (ordinal) | Size = S/M/L | Encode preserving order |
| Label encoding | One-hot encoding | |
|---|---|---|
| Result | One column of integers | One binary column per category |
| Order | Implies order (0 < 1 < 2) | No implied order |
| Use for | Ordinal categories | Nominal categories |
| Normalization (Min-Max) | Standardization (Z-score) | |
|---|---|---|
| Formula | (x - min) / (max - min) | (x - mean) / std |
| Range | 0 to 1 | Centered on 0, unit variance |
| Sensitive to | Outliers | Less sensitive to outliers |
Scaling matters for distance-based and gradient-based models, where a feature with a huge range can dominate others.
These operations are what pandas/Sklearn do under the hood. Pure Python makes the math visible; real workflows use the optimized libraries.
Cleaning a small customer table.
(duplicates removed, missing imputed, case normalized, outlier treated)