MACHINE LEARNING • LESSON 6

Preparing a Real Dataset

Real-world data is rarely ready to use immediately. It may contain missing values, duplicates, incorrect values, categorical data, different numerical scales, and other problems. Before training a model, we need to prepare the dataset carefully.

THE SIMPLEST IDEA

Real data needs to be cleaned and prepared before a model can learn from it.

Data preparation means turning messy real-world data into a clean and usable dataset for machine learning.

01

Start With Raw Data

Imagine an e-commerce company gives us this customer dataset.

Age City Income Orders
25 Hyderabad ₹50,000 5
31 Mumbai ₹80,000 3
29 Hyderabad Missing 4
29 Hyderabad Missing 4
250 Mumbai ₹70,000 6

At first glance, the dataset looks simple. But there are several problems hidden inside it.

Missing income
Duplicate row
Age = 250
City is categorical

This is why we should not immediately send raw data into a machine learning model.

02

Step 1 — Find Problems in the Data

First, inspect the dataset and identify problems.

RAW DATA Customer dataset
INSPECT Find data problems
PREPARE Fix the problems

Typical things to check include:

Missing values
Duplicate records
Incorrect values
Outliers
Categorical features
Different feature scales
03

Step 2 — Handle Missing Values

Our dataset contains a missing Income value.

BEFORE Income = Missing
AFTER Missing value handled

Depending on the situation, we might remove the row, replace the value with a suitable statistic such as the median, or use another appropriate method.

The important point is that the missing value should not simply be ignored without considering how it affects the model.

04

Step 3 — Remove Duplicate Data

Our raw dataset contains the same customer record twice.

Age: 29 Hyderabad Income: Missing Orders: 4
Age: 29 Hyderabad Income: Missing Orders: 4

If these really represent the same record, keeping both can give that observation extra influence.

We should identify and handle duplicates before training the model.

05

Step 4 — Handle Incorrect Values and Outliers

Our dataset contains:

AGE 25, 31, 29

Reasonable values

AGE 250

Suspicious value

An age of 250 is almost certainly an incorrect value. We should investigate and correct or remove it.

Remember the distinction from the previous topic: an unusual value is not automatically wrong. We need to investigate before removing an outlier.

06

Step 5 — Encode Categorical Data

Our City column contains text:

Hyderabad
Mumbai
Chennai

These are categorical values. Depending on the situation, we can encode them so that the machine learning model can use them.

ORIGINAL City

Hyderabad / Mumbai / Chennai

ENCODED Numerical features

Suitable representation for the model

07

Step 6 — Scale Numerical Features

Suppose our final numerical features include:

Age 18 – 80
Orders 1 – 50
Income ₹20K – ₹20L

These features have very different numerical ranges.

For algorithms that are sensitive to feature scale, we may apply an appropriate scaling method.

Scaling changes the numerical representation, not the underlying meaning of the feature.
08

Step 7 — Split the Dataset

After deciding how to prepare the data, we need to keep separate data for evaluation.

PREPARED DATA Complete usable dataset
TRAINING Learn patterns
TEST Evaluate performance

The exact split depends on the problem and dataset. A common example is 80% training and 20% testing.

Most importantly, test information should not leak into the training process.

09

The Complete Preparation Process

Now we can see how the individual preparation steps fit together.

Raw Dataset
Find Problems
Handle Missing Values
Remove Duplicates
Fix Incorrect Data
Handle Outliers
Encode Categories
Scale Features When Needed
Split Data Correctly
10

A Simple Python Example

In Python, a real data-preparation workflow might look conceptually like this:

import pandas as pd
from sklearn.model_selection import train_test_split

# Load the dataset
data = pd.read_csv("customers.csv")

# Remove duplicate rows
data = data.drop_duplicates()

# Separate features and label
X = data.drop("purchased", axis=1)
y = data["purchased"]

# Split the data
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.2,
    random_state=42
)

This is only a simplified example. Real datasets may require additional cleaning, encoding, missing-value handling, scaling, and other preprocessing steps.

read_csv() Loads the dataset.
drop_duplicates() Removes duplicate rows.
X Contains the input features.
y Contains the target we want to predict.
train_test_split() Separates data for training and testing.
REMEMBER THIS

Good Models Need Good Data.

Data preparation is not about blindly changing every value. It is about understanding the dataset, identifying problems, fixing them appropriately, converting data into usable features, and keeping evaluation data separate.

QUICK CHECK

What Should We Do With This Dataset?

Problem Example First Action Why?
Missing value Income = missing Investigate / handle Model needs usable data
Duplicate Same row twice Identify duplicate Avoid duplicated observations
Incorrect value Age = 250 Investigate / correct Value may be invalid
Category City = Hyderabad Encode Convert to usable representation
Answer

Do not immediately train the model. First inspect the data, handle the problems appropriately, encode categorical information when needed, scale numerical features when appropriate, and split the data correctly for training and evaluation.

LESSON 6 COMPLETE

Data Preparation Is Done

You now understand the main steps involved in preparing data for machine learning: missing values, duplicates, incorrect data, outliers, categorical encoding, feature scaling, and correct data splitting.

Next, we move from data preparation into our first major machine learning algorithm: Linear Regression.