Okay, my friends.
Are you ready for the final tool of our data preprocessing toolkit
feature scaling, which will allow us to put all our features on the same scale.
So that's the what?
And let me quickly remind the why.
Why do we need to do this?
Well, that's because for some of the machinery models,
that's in order to avoid some features to be dominated by other features
in such a way that the dominated features are not even considered by the machine
model.
Now, you also need to be aware that we will not have to apply feature
scaling for all the machinery models just for some of them.
Therefore, we won't include this in our data preprocessing template,
which I will show you by the way, at the end of this tutorial.
So we will just add this tool in the toolkit
because indeed for a lot of machinery models, we won't even have to apply
feature scaling even if we have features taking very different values.
For example, if you already know a bit about the multiple linear
regression model, you know that each variable is actually multiplied
by a coefficient, you know, in the linear regression equation.
And so well, you know,
if you have variables that take much higher values than others.
Well, when learning the coefficients, the coefficients will just compensate
by taking small values for the variables that take high values.
Right.
We will explain this more in part to regression.
But for now, just know that this is a tool that will be applied
from time to time for certain machine learning models, but not all the time.
As you will see in this course. All right.
And also in the previous tutorial, I actually told you that
I was about to answer one of the most important questions
or most frequently asked questions by the data science community.
And I will keep, of course, my promise.
I will answer this when it's time.
Meaning, at around the middle of this tutorial.
But no worries, all questions about feature scaling
will be answered so that you have absolutely no confusion.
All right, so we have the what and the why.
And now let's proceed to the how.
Meaning how are we going to apply feature scaling.
And to answer this question I'm going to show you the following slides
which are the main two feature scaling techniques
that indeed put all your features in the same scale.
And these two techniques are first standardization,
which consists of subtracting each value of your feature
by the mean of all the values of the feature, and then dividing
by the standard deviation, which is the square root of the variance.
And this will put all the values of the feature
between around minus three and plus three, right?
All the different features.
When you apply this transformation on all the features of your data set,
well, all your features will take value between around minus three and plus three.
So that's standardization.
And then you have normalization which consists of subtracting each value
of your feature by the minimum value of the feature,
and then dividing by the difference between the maximum value of the feature
and the minimum value of the feature.
And so since this is positive, this is positive.
And this is always larger than this.
Well that means that all the values of your features
will become between 0 and 1.
All right.
So this will result in having
values of features between minus three and plus three more or less.
And this will result in having all the values of your features between 0 and 1.
Now the question is also much asked by the data science community.
Should we go for standardization or normalization?
Well, we're going to be here very pragmatic.
Normalization is recommended
when you have a normal distribution in most of your features.
This will be a great feature scaling technique.
In that case standardization actually works well all the time.
It will do the job all the time.
Therefore, since this is a technique that will work all the time,
and this is a technique that is more recommended for some specific situations
where you have most of your features following a normal distribution.
Then my ultimate recommendation for sure
is to go for standardization, because indeed this will always work.
You will always do some relevant feature scaling,
and this will always improve the training process.
So I'm going to teach you this technique.
I'm going to teach you on how to apply it on R matrices and features.
And I'm seeing matrices of features because now
we have two matrices of features which are Xtrain and Exodus.
And since we understood in the previous tutorial
that feature scaling must be applied after the split, well, you understand
that we want apply features getting on the whole matrix of features x,
but of course on both x train and X test separately
and actually the scaler will be fitted to only X train.
And then we'll transform X test.
You know we'll apply feature scaling on access
because indeed since X test is something that's we're not supposed to have
during the training, but only after like when going in production.
Well, we're not allowed to fit our feature scaling tool on the test set right
by fitting the feature scaling to on the test set,
that means that we're going
to get the mean of the whole set, and then the standard deviation in the feature.
No, we don't have the right to do this because x
this is supposed to be something new.
And therefore we'll just get the mean of the values in Xtrain
then get the standard deviation of the values next train.
Then apply this formula to transform all the values in Xtrain
and then apply that same formula, but with the same mean and standard deviation
of the values in Xtrain to scale the values of x.
This. It's really, really important that you understand this.
And this is once again, to give some further elements of response
to that previous question.
Should we scale before or after the split?
All right.
So now that we are all clear on this, let's proceed to the implementation
of the how, meaning the implementation of feature scaling.