Welcome back.
We succeeded in splitting the data into test and training sets.
Now we're going to introduce a very important concept called feature scaling.
Feature scaling is especially crucial for neural networks and many other machine learning algorithms.
It standardizes the range of independent features in the data to a common scale.
Back to our model.
We have the input.
In the input layer we have nine parameters cylinders.
Displacement.
Horsepower, weight, acceleration, model, year, origin one, two and three.
So if we look at cylinders parameter we have numbers and the range is between 3 and 9 while the displacement
is different scale which is CC and it's range from six 8 to 4 five five, while the horsepower is another
Measurement and another feature with horsepower, HP and range 46 to 230, while the weight is in pounds
ranging from 1613 to 5 one, four zero and so on.
And the other features are the same.
So those features have different scales and different units.
How to measure the effect.
In a one scale, we need to use one scale called feature scaling and make them as a one unit common
scale.
So feature scaling standardizes the range of the independent features in the data to a common scale.
So here we need to create a common scale to measure the features because they are different.
Why this is very important for neural network for the faster convergence.
Gradient descent converges much faster when features are on similar scales.
So when when dealing and when giving those data to the machine learning, it will learn faster.
When the features are on similar scales.
Without scaling, the algorithm zigzags towards the optimum.
It will not reach the optimum.
Also, we prevent dominance.
Prevents large scale features like the weight.
Look at the weight.
We have £303,500 for our for our example, and the range is between 1613 and 2 5140.
And this is a large scale compared to the cylinders displacement, horsepower, model year, origin
and so on.
So the dominant here is wait.
We're not going to like detect and predict the MPG based only on the weight because the weight is the
dominance here.
So here we need to prevent large scale features like weight from dominating small scale features like
acceleration, cylinders, displacement, horsepower, model year, origin, and so on.
All features contribute equally to the learning process, so the MPG depends on acceleration, model
year, weight, horsepower, displacement, and cylinders equally.
All feature contribute equally to the learning process, reducing numerical precision.
Precision issues during calculations and prevents preventing gradient, explosion or vanishing problems
that we will see in the next videos.
Okay, so the main concept here is to standardize all the features to one scale.
To do that we need to create a scalar object.
So here we have to use scalar standard scalar.
What does standard scalar do.
Standard scalar transforms or standard scalar.
Sorry.
What does standard scalar do.
It transforms data to have mean equals to zero and standard deviation equals to one.
With a formula x minus mean over standard deviation, x is the feature value over a minus mean over
standard deviation.
For example, here we have the weight feature.
Before scaling we have 1000 for the first car 3,002nd card and so on.
After scaling, we need to create a scale that allows us to measure the weight according to the mean
and the standard deviation.
So mean equals to zero and standard deviation equals to one.
We use this formula to transform the 1500 to -1.2, and 3000 to 1.5, 2500 to 0.8, and so on.
Okay.
So this is the the scaling.
This is before scaling and this is after scaling.
Now we need to use the fit transform function x train and scaled.
So this is the annotation I want from you to fill with scalar dot fit transform.
And here we need to train x.
Here we are calculating the mean and standard deviation from training data only then Transforming the
training data.
So here we use the training data, not the testing data, because we need to test our data in a reliable
mode in a reliable way.
We don't need to pass any data before for the training.
Also, we used the X train in order to calculate the mean and the standard deviation from training data
only.
Then let's use the transform function x scaled equals to scalar dot transform x test.
This will use the same mean and standard deviation from training data doesn't fit on test data.
So those are very important.
Here we are creating the scaler by using this formula.
X minus mu over standard deviation.
Having the mean equals to zero and standard deviation equals to one.
So those data if we make it, we have the if we calculate, we get mean equals to zero and standard
deviation one.
Before scaling, the weight was this.
And after scaling the weight becomes this okay without scaling problems.
Neural network train very slow.
So without using the trained scaled feature and calculating the mean and standard deviation from training
data only, then transforming the training data using the fit, transform and transform functions.
Neural networks train very slowly.
Some features dominate, others model may not converge properly, and poor performance.
Feature scaling is a critical pre-processing step that often makes the difference between a model that
works well and one that doesn't.
So this is a very, very important thing.