0% found this document useful (0 votes)
2 views9 pages

Notes_Data_Preprocessing_NumPy_Beginner

The document provides a comprehensive guide on data preparation and preprocessing techniques using NumPy for beginners, highlighting the importance of cleaning and transforming raw data for effective machine learning. It covers techniques such as binarization, mean removal, scaling, and normalization, explaining their purposes, methods, and providing code examples. Additionally, it includes common mistakes to avoid and a practice exercise to apply the discussed techniques.

Uploaded by

Anusha Av
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Notes_Data_Preprocessing_NumPy_Beginner

The document provides a comprehensive guide on data preparation and preprocessing techniques using NumPy for beginners, highlighting the importance of cleaning and transforming raw data for effective machine learning. It covers techniques such as binarization, mean removal, scaling, and normalization, explaining their purposes, methods, and providing code examples. Additionally, it includes common mistakes to avoid and a practice exercise to apply the discussed techniques.

Uploaded by

Anusha Av
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Detailed Notes: Data Preparation &

Preprocessing using NumPy

Binarization | Mean Removal | Scaling | Normalization

(For Beginners)

1. Why Do We Need Data Preprocessing?

Real-world data is often messy — it can have different scales, units, or


formats. For example, one column might have values like "age" (0–100)
while another has "income" (0–1,00,000). If we feed this raw,
unprocessed data directly into a Machine Learning algorithm, the
algorithm may give more importance to the column with larger
numbers, even if it's not actually more important. This leads to poor or
biased results.

Data Preprocessing is the step where we clean, transform, and prepare


raw data before giving it to a Machine Learning model, so that:

All features are on a comparable scale

The data is free from unnecessary bias

The algorithm can learn patterns more accurately and efficiently

NumPy (Numerical Python) is a Python library used to handle arrays


and perform mathematical operations efficiently — making it a core
tool for data preprocessing. It is often used along with
[Link] for ready-made preprocessing functions.

2. Setting Up
Before starting, make sure you import the required libraries:

import numpy as np
from sklearn import preprocessing

We will use this sample raw dataset throughout the notes (a 3×4
NumPy array — think of it as 3 rows/samples and 4 columns/features):

data = [Link]([[3, -1.5, 2, -5.4],


[0, 4, -0.3, 2.1],
[1, 3.3, -1.9, -4.3]])
print(data)

Output:

[[ 3. -1.5 2. -5.4]
[ 0. 4. -0.3 2.1]
[ 1. 3.3 -1.9 -4.3]]

3. Technique 1: Binarization

What is it?

Binarization is the process of converting numerical data into binary


values — 0 or 1 — based on a fixed threshold.

Why do we use it?

Sometimes we don't care about the exact value, only whether it crosses
a certain point or not. For example: "Did the student score above 40
marks (pass) or not (fail)?" This is useful when we want to convert
continuous data into a simple yes/no (1/0) format.

How it works:

Choose a threshold value.


Any value greater than the threshold becomes 1.

Any value less than or equal to the threshold becomes 0.

Step-by-step example:

If threshold = 1.4:

3 → greater than 1.4 → 1

-1.5 → less than 1.4 → 0

2 → greater than 1.4 → 1

-5.4 → less than 1.4 → 0

Code:

binarizer = [Link](threshold=1.4)
binarized_data = [Link](data)
print(binarized_data)

Output:

[[1. 0. 1. 0.]
[0. 1. 0. 1.]
[0. 1. 0. 0.]]

Beginner Tip:

Think of binarization like a light switch — if the value is "high enough,"


the switch turns ON (1); otherwise, it stays OFF (0).

4. Technique 2: Mean Removal

What is it?

Mean Removal (also called centering) is the process of subtracting the


mean (average) of each feature/column from every value in that
column, so that the new mean becomes 0.
Why do we use it?

Raw data often has a bias — some features might naturally have higher
average values than others. Removing the mean eliminates this bias
and centers the data around zero, which helps many ML algorithms
perform better and converge faster.

How it works:

For each column:

1. Calculate the mean of that column.

2. Subtract the mean from every value in that column.

Formula: new_value = old_value - mean_of_column

Step-by-step example (for column 1: 3, 0, 1):

Mean = (3 + 0 + 1) / 3 = 1.33

New values = 3 - 1.33 = 1.67, 0 - 1.33 = -1.33, 1 - 1.33 = -0.33

Code:

print("Original mean:", [Link](axis=0))

mean_removed_data = data - [Link](axis=0)


print("Mean removed data:\n", mean_removed_data)
print("New mean:", mean_removed_data.mean(axis=0))

Output (new mean should be very close to 0 for each column):

New mean: [0. 0. 0. 0.]

Beginner Tip:

axis=0 means "calculate down each column." If you used axis=1 , it


would calculate across each row instead — always double check which
axis you need.
5. Technique 3: Scaling

What is it?

Scaling adjusts the values of each feature so they fall within a specific,
fixed range — most commonly [0, 1]. This is often called Min-Max
Scaling.

Why do we use it?

Different features may have very different ranges (e.g., age: 0–100,
salary: 0–1,00,000). Scaling brings all features to the same range so no
single feature dominates just because of its larger numeric scale.

How it works (Min-Max Scaling Formula):

scaled_value = (value - min_value) / (max_value -


min_value)

This formula converts the minimum value of a column to 0, the


maximum value to 1, and everything else proportionally in between.

Step-by-step example (for column 1: 3, 0, 1):

min = 0, max = 3

3 → (3-0)/(3-0) = 1

0 → (0-0)/(3-0) = 0

1 → (1-0)/(3-0) = 0.33

Code:

data_scaler = [Link](feature_range=
(0, 1))
scaled_data = data_scaler.fit_transform(data)
print("Scaled data:\n", scaled_data)
Output:

[[1. 0. 1. 0. ]
[0. 1. 0.41... 1. ]
[0.33... 0.87... 0. 0.17... ]]

Beginner Tip:

After scaling, the smallest value in each column becomes 0 and the
largest becomes 1 — everything else is "stretched" proportionally
between them.

6. Technique 4: Normalization

What is it?

Normalization adjusts the values in each row (sample) so that they fit
within a common scale — typically so that each row has a unit norm
(i.e., the "length" of the row vector becomes 1).

Why do we use it?

Normalization is especially useful when the magnitude of the data


matters less than the pattern/direction of the data — commonly used
in text classification, recommendation systems, and clustering, where
we want to compare data based on proportion rather than absolute
size.

Types of Normalization:

L1 Normalization: Sum of absolute values in each row = 1

L2 Normalization: Sum of squares of values in each row = 1


(Euclidean norm)

How it works (L1 example):

For a row: [3, -1.5, 2, -5.4]


1. Take the sum of absolute values: |3| + |-1.5| + |2| + |-5.4| = 11.9

2. Divide each value by this sum: 3/11.9, -1.5/11.9, 2/11.9, -5.4/11.9

Code:

# L1 Normalization
l1_normalized = [Link](data, norm='l1')
print("L1 normalized data:\n", l1_normalized)

# L2 Normalization
l2_normalized = [Link](data, norm='l2')
print("L2 normalized data:\n", l2_normalized)

Output (L1 — each row's absolute values sum to 1):

[[ 0.25210084 -0.12605042 0.16806723 -0.45378151]


[ 0. 0.625 -0.046875 0.328125 ]
[ 0.0952381 0.31428571 -0.18095238 -0.40952381]]

Beginner Tip:

Think of normalization as resizing each row to the "same length" while


keeping its direction/pattern — like resizing photos to the same file size
while keeping the same picture.

7. Quick Comparison Table

Works Common Use


Technique Goal Formula/Idea
On Case

value > Pass/Fail,


Whole Convert
Binarization threshold → Yes/No
array to 0/1
1, else 0 decisions

Mean Each Center value - mean Removing bias


Removal column data before modeling
Works Common Use
Technique Goal Formula/Idea
On Case

around
0

Fit
Comparing
Scaling (Min- Each values (value - min)
features with
Max) column into / (max - min)
different ranges
[0,1]

Make
row Text data,
Each value /
Normalization vectors recommendation
row norm(row)
unit systems
length

8. Common Beginner Mistakes to Avoid

Confusing Scaling (works per column, fixed range) with


Normalization (works per row, unit norm) — they are NOT the same
thing.

Forgetting to specify axis=0 vs axis=1 when computing


mean/sum — this changes whether you're working with columns or
rows.

Applying preprocessing techniques in the wrong order, or


forgetting to apply the same scaling parameters (min, max, mean)
to test data as were used on training data.

Choosing a threshold for Binarization without understanding what


it represents in the real dataset.

9. Summary
Data Preprocessing prepares raw data to make ML algorithms
perform better.

Binarization converts values into 0/1 using a threshold.

Mean Removal centers data around zero by subtracting the


column mean.

Scaling brings values into a fixed range like [0,1] using Min-Max
formula.

Normalization adjusts each row to have a unit norm — useful when


the direction/pattern of data matters more than magnitude.

NumPy, along with [Link] , provides simple


built-in tools to perform all these operations efficiently.

10. Practice Exercise

Using the array below, apply all four preprocessing techniques and
observe the differences in output:

practice_data = [Link]([[10, 2, -3],


[5, -6, 8],
[-2, 9, 1]])

1. Binarize with threshold = 3

2. Remove the mean (verify new mean ≈ 0)

3. Apply Min-Max scaling to range [0, 1]

4. Apply L2 normalization and verify each row's magnitude ≈ 1

You might also like