All right, my friends, let's do this.
Let's proceed to the next tool in our data
preprocessing toolkit, which is about encoding categorical data.
So first let me explain why we have to do this.
Let's open the data set again.
And as we can see this data set contains one column
with categories, you know France, Spain or Germany.
First you might guess that it will be difficult for machine learning model
to compute some correlations between these columns.
You know, the features and the outcome, which is the dependent variable.
And therefore of course we will have to turn these strings,
you know, these categories into numbers.
So one idea would be to encode France
into zero, Spain into one and Germany into two.
However, if we do this, our future machine learning model could understand
that because France is zero, Spain is one and Germany's two.
There is a numerical order between these three countries,
and mostly it could interpret that this order
matters, whereas of course it is absolutely not the case.
Right?
There is not a relationship order
between these three countries France, Germany and Spain.
So we want to avoid the model to have such an interpretation,
because that could cause some misinterpreted correlations
between the features and the outcome, which we want to predict.
Therefore, we can actually do much better than just
encode these three countries into zero, one, and two.
And this thing that we can do better is actually one hot encoding.
And one hot encoding consists of turning this
country column into three columns y three columns.
Because there are actually three
different classes in this country column, you know, three different categories.
If there were, for example, five countries here, we would turn this column
into five columns.
And one hot encoding consists of creating
binary vectors for each of the countries.
Let me explain this right away.
So very simply, France would, for example, have the vector 100,
Spain would have the vector 010
and Germany would have the vector 001, so that then
there is not a numerical order between the three countries,
because instead of having zero, one and two,
we would only have zeros and ones and therefore three new columns.
I'm going to show you, of course, what we're going to create.
We're basically going to replace this country column by three new columns
containing the zeros and ones encoding each of the countries.
That is called one hot encoding.
And that is a very useful and popular method to use
when pre-processing your data sets containing categorical variables.
So that's the first thing we'll do here for this country column.
And then remember that there is also this purchased columns
that has labels, you know, non-numerical values with yes nos.
And we will actually have to replace them by zeros and ones.
And that's totally fine for the dependent variable.
As long as it is a binary outcome, it is super fine.
It will actually not compromise the future accuracy of the model.
If you just replace no and yes by zero and one.
Okay, so I will teach you how to do these two things.
And first let's start by one hot encoding the country column here.
And there we go.
Let's create a new code cell for this new step.
And coding the independent variable.
All right.
So to do this we're going to use two classes.
The first one is the column transformer class
from the compose module of once again the scikit learn library.
And the second class is the one hot encoding class
from the preprocessing module of the same scikit learn library.
So first let's import these two classes.
So we have to take them from scikit learn
from which we're going to call first the compose module.
There we go from which we're going to import
that class we're interested in which is as Google Collab.
Guess it's perfectly gone transform it.
And then from scikit learn, once again
we're going to get access to the pre-processing module perfect,
from which we're going to import the one hot encoder class.
And now we're going to mix these two classes in order to do this.
One hot encoding on the country column.