Data Preprocessing

What Is Data Preprocessing?

Data preprocessing is the process of transforming raw data into a format that a machine learning model can understand and learn from effectively.

Common Preprocessing Steps
  • Data type conversion
  • Normalization
  • Standardization
  • Encoding categorical data
  • Tokenization (for text)
  • Image resizing and normalization
  • Train/validation/test splitting

Data Flow

01

Raw Data — Collected from various sources

02

Preprocessing — Transform into clean numeric form

03

Model Training — Model learns from preprocessed data

04

Predictions — Model makes predictions on new data

Data Type Conversion

Machines need numbers. Text, dates, and categories must be converted before most models can use them.

Conversion Examples

Date text — "2026-06-09" → timestamp number

Yes/No — "Yes" → 1, "No" → 0

Categories — "Red", "Blue", "Green" → 0, 1, 2

Normalization

Normalization scales values to a fixed range, usually between 0 and 1. This ensures no single feature dominates simply because of its scale.

Original → Normalized Values

50 → 0.00

100 → 0.33

150 → 0.66

200 → 1.00

Formula: (value - min) / (max - min)

Why Normalization Is Needed

Feature 1 — Age, range 0–100

Feature 2 — Income, range 20,000–200,000

Without normalization the model may overweight income simply because the numbers are larger.

Min-Max Example

(25 - 0) / (100 - 0) = 0.25

When to Use Normalization
  • When you want all features between 0 and 1
  • When the algorithm is sensitive to feature scale (KNN, Neural Networks)
  • When data does not follow a normal distribution

Standardization

Standardization transforms data so the mean is 0 and the standard deviation is 1. It does not require knowing the min and max values.

Formula Example

Score — 90

Class mean — 80

Standard deviation — 10

Standardized — (90 - 80) / 10 = 1.0

A value of 1.0 means this score is 1 standard deviation above the class average.

When to Use Standardization
  • When data follows a roughly normal distribution
  • When using algorithms that assume normality (Logistic Regression, SVM)
  • When features have very different ranges and units

Normalization vs Standardization

NormalizationStandardization
Scales to 0–1 rangeCenters around mean 0
Requires min and maxUses mean and standard deviation
Best for non-normal distributionsBest for roughly normal distributions
Good for KNN, Neural NetworksGood for Logistic Regression, SVM
Affected by outliersMore robust to outliers

Encoding Categorical Data

Machine learning models cannot directly use text labels. Categories must be converted to numbers.

Categorical Examples
  • Color: Red, Blue, Green
  • Country: Canada, USA, Mexico
  • Animal: Cat, Dog, Bird

Label Encoding vs One-Hot Encoding

Label Encoding

Red = 0, Blue = 1, Green = 2

Simple but may imply ordering between categories.

Best for — Ordinal categories (Small, Medium, Large)

One-Hot Encoding

Red → [1, 0, 0]

Blue → [0, 1, 0]

Green → [0, 0, 1]

Best for — Non-ordinal categories (Color, Country)

Tokenization

Tokenization splits text into smaller units called tokens. This is an essential step in natural language processing.

Example

Sentence — "The cat sat on the mat"

Tokens — ["The", "cat", "sat", "on", "the", "mat"]

Tokenization Is Used In
  • Language models
  • Sentiment analysis
  • Machine translation
  • Text classification

Text Preprocessing Steps

01

Convert text to lowercase

02

Remove punctuation and special characters

03

Tokenize into individual words

04

Remove stop words (the, a, is, and)

05

Apply stemming or lemmatization

Stop Words Example

Original — "The cat sat on the mat"

After removing stop words — ["cat", "sat", "mat"]

Stemming vs Lemmatization

Stemming

Cuts words to a base form (may not be a real word)

running → run

studies → studi

Faster but less accurate

Lemmatization

Converts to the proper dictionary form

running → run

studies → study

Slower but more accurate

Image Preprocessing

Images must be preprocessed before most models can use them. Images come in different sizes, and models need a consistent input size.

Resizing

Original image — 1920 × 1080 pixels

Resized to — 224 × 224 × 3 (height × width × color channels)

Pixel Normalization

Pixel values range from 0 to 255

Example — 128 / 255 = 0.50

Normalizing to 0–1 helps training stability and speed

Image Augmentation Examples
  • Flip horizontally
  • Rotate slightly
  • Adjust brightness
  • Zoom in or out
  • Add random noise

Audio and Video Preprocessing

Audio Preprocessing
  • Convert to spectrograms
  • Normalize amplitude
  • Resample to standard frequency
  • Remove silence
Video Preprocessing
  • Extract individual frames
  • Resize frames to model input size
  • Process audio separately
  • Subsample frames if video is long

Train / Validation / Test Split

Example — 10,000 examples

Training (70%) — 7,000 examples — used to teach the model

Validation (15%) — 1,500 examples — used to tune the model

Test (15%) — 1,500 examples — used to evaluate final performance

Data Leakage in Preprocessing

Mistake Approach

Calculate normalization values using the entire dataset including test data.

Problem — Test data has influenced training setup (data leakage)

Better Approach

Calculate normalization values using only the training set. Apply those same values to transform validation and test data.

Preprocessing Pipeline

01

Handle missing values

02

Encode categorical features

03

Normalize or standardize numerical features

04

Tokenize text (if applicable)

05

Split data into train, validation, and test sets

Consistency — Training vs Deployment

Example

If you normalized age between 18 and 80 during training, you must apply the same formula (same min and max values) when a new user's age is entered at prediction time.

Inconsistent preprocessing can cause silent failures in production.

Real-World Examples

House Price Preprocessing
  • Scale house size and price to 0–1
  • Encode city names using one-hot encoding
  • Handle missing location values before encoding
Spam Detection Preprocessing
  • Lowercase all text
  • Remove punctuation
  • Tokenize into words
  • Remove stop words
  • Convert to word vectors
Image Classification Preprocessing
  • Resize all images to 224×224
  • Normalize pixel values to 0–1
  • Apply augmentation during training only

Common Mistakes

Mistakes to Avoid
  • Using test data to calculate normalization parameters
  • Applying different preprocessing to training versus test data
  • Forgetting to handle missing values before encoding
  • Normalizing the entire dataset before splitting (causes data leakage)

Key Takeaways

Summary
  • Preprocessing transforms raw data into model-ready form
  • Normalization scales values between 0 and 1
  • Standardization centers data around mean 0
  • Encoding converts categories into numbers
  • Tokenization splits text into smaller units
  • Always split data before calculating preprocessing parameters
  • Apply the same preprocessing consistently during training and deployment

Need Help?

Ask the AI if you need help understanding normalization, standardization, encoding, tokenization, image preprocessing, or how to avoid data leakage during preprocessing.