Feature Scaling
Lesson Introduction
Hey there! Today, we're going to learn about feature scaling. You might be wondering, what is feature scaling, and why should we care? Simply put, feature scaling is like making sure all the ingredients in your recipe are measured in the same unit. Imagine trying to mix pounds of flour and teaspoons of salt without converting one to the other — it wouldn't make sense, right?
Our goal is to understand why feature scaling is crucial in machine learning and to learn how to do it using Python and a library called SciKit Learn.
What is Feature Scaling?
Feature scaling ensures that all your data features contribute equally when building a machine learning model. Without scaling, large values might dominate, leading to biased outcomes. For example, if predicting house prices, and one feature was in thousands (like square footage) and another in single digits (like the number of rooms), the model might overlook the smaller values just because they seem less relevant.
There are two common types:
-
Standardization: Transforms data to have a mean () of 0 and a standard deviation () of 1.
Formula: , where is the original feature value, is the mean of the feature, and is the standard deviation of the feature.
-
Normalization: Rescales data to range between 0 and 1.
Formula: , where is the original feature value, is the minimum value of the feature, and is the maximum value of the feature.
Today, we'll focus on both standardization using StandardScaler and normalization using MinMaxScaler from SciKit Learn.
Example of Feature Scaling with `StandardScaler`
Let's create a small sample dataset to see how feature scaling works.
Output:
Before scaling, Feature1 ranges from 1 to 4, and Feature2 ranges from 10 to 40. Let's scale this dataset using StandardScaler.
