All right.
So now all good.
Now we apply correctly this transformation to one hot
encode the columns of this matrix of features x.
Let's check that right away by creating a new code cell
and then printing this matrix of features x.
There we go.
Now let's run this cell.
And then running the next one.
And what do we see.
Well we see exactly what was expected here.
Meaning that we no longer have the first country column with the text.
You know, the three countries here and strings.
This time we have, as we said, three new columns.
So I don't know if you can see it, but that's the first one.
That's the second one.
And that's the third one where each row encodes one of the three countries.
So actually France, you know, that was the first row.
France is encoded as one zero and zero.
You know a vector of one zero and zero.
Then Spain is encoded as a vector of zero zero and one.
And Germany is encoded as a vector of zero one and zero.
You see the unique idea for each of these three countries.
Thanks to these three columns here.
And that's exactly the idea of one hot encoding.
We not only turn our countries
into numerical values, but also there is not a numerical order.
Thanks to these zeros and ones here in three columns.
So that's perfect.
And that will provide the best results for our future machine learning models.
All right. So congratulations.
Now you know how to one hot encode some categorical data.
Now we're going to quickly do another encoding
for the dependent variable because indeed it has a text format.
No. And yes.
And we would just like to convert these strings into zero and one respectively.
And to do this well that's very simple.
We're going to use another class called label encoder and which will
exactly encode these nos and yeses into zeros and ones respectively.
All right.
So let's do this.
Let's create a new code cell here.
Let's scroll down a bit.
Here we go.
And now let's encode the dependent variable.
So as we said we're going to use the label encoder class which we get
from the scikit learn library again from which we're going to call
the preprocessing module from which we're going to import.
Well there we go the label encoder class.
Then exactly as what we did before, we're going to create
an object of this class which we're going to call elite.
And to do this well we simply need to call the class label encoder.
There we go then some parentheses.
Then good news.
We don't have to input anything in the parentheses
because you know we will just need to enter directly. Why.
Because it is only one single vector.
So it will be obvious what will be needed to encode.
And so there we go. Let's do this.
Let's first call our object early from which we're going to call.
Well good news.
Once again there is a fit transform method
which we can call directly on Y
and which will exactly convert the nose.
And yes, you know the text inside numerical values.
And this time we don't have to have a numpy array
because this is the dependent variable vector.
It doesn't need to be a numpy array.
You know as what is expected by the future machinery models.
So we can just set the new Y to be what is returned by this fit
transform method applied to the old way with the Texas nose.
And yes.
All right, let's check this.
Let's create a new code cell just to print
our dependent variable vector y.
Let's run this cell first and then this one.
And let's see if we get zeros and ones.
And yes, indeed we get the zeros and the ones.
Right. No. Corresponds to zero.
And then one here correspond to. Yes. Right.
Then it should be 0011000110.
Okay. So all good.
So now you not only know how to apply one hot encoding
when you have several categories.
And one of the features of your matrix of features.
But also you can do a simple label
encoding when you have, you know, two classes which you can directly encode
into zeros and ones, you know, a binary outcome.
All right. Perfect. Congratulations.
Now you have an extra tool in your data preprocessing toolkit.
The way to encode categorical data.
And now we're going to move on to the next tool which will be to split
the data set into the training set and test set.
So digest this.
And as soon as you're ready meet me in the next tutorial.