"A great model trained on bad data is like a Formula 1 car on a muddy road: powerful, but going nowhere fast."
Covers the importance and methods of preparing data for machine learning.
Raw data is rarely ready for machine learning. A customer table might contain ages like 29, 31, and unknown, incomes written as 50000 and $50,000, and cities entered as both NYC and New York. A model sees this mess literally.
Data preprocessing is the step where we clean and reshape data so patterns become learnable. Think of it like washing, chopping, and measuring ingredients before cooking: the recipe matters, but prep determines whether the dish works.
Common preprocessing tasks include:
red, blue, greenWithout preprocessing, your model may learn noise, overweight the wrong features, or fail entirely.
Start by finding problems that would confuse a model. Suppose an online store has these rows:
Age = 34, Country = US, Spent = 120.5Age = ?, Country = U.S., Spent = 120.50Age = 340, Country = United States, Spent = 0Here, ? is a missing value, 340 is likely an outlier or typo, and the country labels are inconsistent. Basic cleaning often includes:
US, U.S., and United States to US340A simple rule: clean for meaning, not perfection. You are trying to preserve real signal while removing accidental mess.
After cleaning, we often transform features into forms models handle better. Two common steps are encoding and scaling.
If Color is red, blue, green, many models need numbers, so we use one-hot encoding:
red → [1,0,0]blue → [0,1,0]green → [0,0,1]For numeric features, scaling prevents large units from dominating. A common method is min-max scaling:
x_scaled = (x - min) / (max - min)
Example: if house sizes range from 800 to 2800 square feet, then a house of 1800 becomes:
(1800 - 800) / (2800 - 800) = 1000 / 2000 = 0.5
Now size sits neatly between 0 and 1, making it easier for many algorithms to compare features fairly.
In practice, preprocessing is a repeatable pipeline, not a random list of fixes. A common order is: inspect, clean, transform, then train.
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# fill missing ages with median
df['age'] = df['age'].fillna(df['age'].median())
# standardize country labels
df['country'] = df['country'].replace({'U.S.': 'US', 'United States': 'US'})
# one-hot encode a category
df = pd.get_dummies(df, columns=['color'])
# scale a numeric column
scaler = MinMaxScaler()
df[['income']] = scaler.fit_transform(df[['income']])
One crucial habit: fit preprocessing steps using training data only, then apply the same rules to validation or test data. Otherwise, you accidentally leak future information into training and get overly optimistic results.
Test your understanding with inline MCQs, track your mastery score, and get scheduled review reminders — free.
Start learning for free