MACHINE LEARNING • LESSON 3

Preparing Data

Data collected from the real world is usually messy. Before giving it to a machine learning model, we need to clean, organize, and transform it into a useful format.

THE CORE IDEA

Raw data is not automatically ready for a model.

Real-world data can contain missing values, duplicate records, incorrect information, unusual values, and text that a model cannot directly understand. Data preparation deals with these problems before training.

01

Why Does Data Need Preparation?

Imagine collecting information about houses to build a house-price prediction model.

Your raw data might look like this:

Size Bedrooms Age Location Price
1500 3 10 New York $300,000
1800 4 7 Boston $380,000
? 3 5 Chicago $320,000
1600 three 8 Boston $350,000

There are already problems here.

  • One house has a missing size.
  • One bedroom value is written as text.

Giving this raw data directly to a model may cause errors or lead to poor results.

02

Step 1: Find Missing Values

Real datasets often have information that is missing.

For example:

RAW DATA House Size = ?
PREPARATION Handle the missing value

Depending on the situation, we might remove the row, fill the value using a reasonable strategy, or use another method.

The important idea is that we should not blindly send incomplete data into the model.

03

Step 2: Remove Duplicate Data

Sometimes the same record appears more than once.

For example, suppose a customer accidentally appears twice in a dataset:

RECORD 1 Customer 101

Order = $200

RECORD 2 Customer 101

Order = $200

If these are actually the same transaction, keeping both can give the model an inaccurate view of the data.

Duplicate records should therefore be identified and handled appropriately.

04

Step 3: Fix Incorrect Data

Data can also contain values that are entered incorrectly.

For example, suppose a dataset contains:

EXPECTED Age = 35
DATA Age = 350
PROBLEM Possible data-entry error

A value like 350 might not make sense if the field represents a person's age.

Data preparation involves identifying these kinds of problems and deciding how they should be handled.

05

Step 4: Handle Categorical Data

Some data is represented using words instead of numbers.

For example:

LOCATION New York
LOCATION Chicago
LOCATION Boston

Many machine learning algorithms work with numerical representations rather than raw text categories.

Therefore, categorical information may need to be transformed into a suitable numerical representation.

Simple idea:

The model needs data in a form that its algorithm can work with.

06

Step 5: Deal With Very Different Scales

Different features can have very different numerical ranges.

For example:

AGE 18 – 80
SALARY 20,000 – 200,000

In some machine learning algorithms, features with very different scales can affect how the model learns.

A technique called feature scaling can be used to bring numerical features onto a more suitable scale.

We will study feature scaling in more detail later.

07

A Simple Before-and-After Example

Let's put the ideas together.

BEFORE Raw Data
  • Missing values
  • Duplicate records
  • Incorrect values
  • Text categories
  • Different numerical scales
AFTER Prepared Data
  • Problems handled
  • Consistent values
  • Useful representation
  • Suitable numerical features
  • Ready for the next ML step
08

Complete Example: House Price Data

Suppose we want to predict house prices.

We collect thousands of houses, but the data is messy.

RAW DATA House information

Size, bedrooms, location, age, price

CLEAN Handle data problems

Missing, duplicate, and incorrect values

PREPARE Transform the data

Make features suitable for the model

Only after this preparation should we move toward training the machine learning model.

09

Do Not Change Data Without Understanding It

Data preparation does not mean deleting anything that looks unusual.

An unusual value may be a genuine real-world value, not an error.

For example, a house that costs $5 million might look unusual compared with most houses in a dataset. That does not automatically mean the value is wrong.

Important:

Always understand the data and the problem before deciding how to modify it.

KEY IDEA

Prepare the Data Before Asking the Model to Learn.

Find and handle problems such as missing values, duplicates, incorrect values, categorical data, and incompatible numerical scales. The goal is to produce data that is suitable for machine learning.

QUICK CHECK

Is This Data Ready?

You are given a customer dataset containing 10,000 records.

Some records are duplicated, some ages are missing, and the city is stored as text such as "New York" and "Chicago".

Can you immediately give this raw dataset to your machine learning model?

Answer

Not necessarily.

The data should first be examined and prepared. Missing values, duplicate records, and categorical information need to be handled appropriately.

NEXT TOPIC

Training a Model

The data has been collected and prepared. Now we can finally give the training data to a machine learning algorithm and let it learn patterns.