manavmodi

Manav Modi

Posted on January 21, 2022

Standardizing Data

#### What is standardization?

It is a preprocessing method used to transform continuous data to make it look normally distributed.

Why?

Scikit-learn models assume normally distributed data. In the case of continuous data, we might risk biasing the models.

Two methods can be used for the standardization process:

  1. Log Normalization
  2. Feature Scaling

These methods are applied to continuous numerical data.

When?

  1. Models are present in linear space. (Ex. KNN, KMeans, etc.),data must also be in linear space.
  2. Dataset features that have high variance. This could bias a model that assumes it is normally distributed.
  3. Modeling dataset that has features that are continuous and on different scales. > For example, a dataset that has height and weight as its features needs to be standardized to make sure they are on the same linear scale.

What is Log Normalization?

  1. Log transformation is applied
  2. Used in datasets where the variance of a particular column is significantly high as compared to other columns
  3. Natural log is applied on values

image.png

  1. It is used to captured relative changes, and magnitude of change, and keeps everything in the positive space.

Let's see the implementation.

print(df) 
Enter fullscreen mode Exit fullscreen mode

image.png

print(df.var())
Enter fullscreen mode Exit fullscreen mode

image.png

We will use the log operator from the NumPy library to perform the normalization.

import numpy as np
df["log_2"] = np.log(df["col2"])
print(df)

Enter fullscreen mode Exit fullscreen mode

image.png
Let's see values.

print(np.var(df[["col1","log_2"]]))

Enter fullscreen mode Exit fullscreen mode

image.png

What is feature scaling?

This method is useful when

  1. continuous features are present on different scales.
  2. model is in linear scale.

The transformation on the dataset is done such that the resultant mean is 0 and the variance is 1.

Here across the features, you can see how the variation is.

print(df)
Enter fullscreen mode Exit fullscreen mode

image.png

print(df.var())
Enter fullscreen mode Exit fullscreen mode

image.png

Using the standardscaler method from sklearn, the process is done.

from sklearn.preprocessing  import StandardScaler
scaler = StandardScaler()
df_scaled= pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
print(df_scaled)
print(df.var())

Enter fullscreen mode Exit fullscreen mode

image.png

image.png

Check out the exercises linked to this here

Interested in Machine Learning content? Follow me on Twitter.

💖 💪 🙅 🚩
manavmodi
Manav Modi

Posted on January 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 1 of 30 : Machine Learning
machinelearning Day 1 of 30 : Machine Learning

April 15, 2024

EXPLORATORY DATA ANALYSIS ULTIMATE GUIDE
machinelearning EXPLORATORY DATA ANALYSIS ULTIMATE GUIDE

February 26, 2023

Standardizing Data
machinelearning Standardizing Data

January 21, 2022

Intro to Machine Learning in Python: Part III
machinelearning Intro to Machine Learning in Python: Part III

January 17, 2021