Hello and welcome to this tutorial.
Okay, so we are halfway our data pre-processing phase.
We learned how to import the libraries.
To import the data set.
We learned how to take care of missing data.
And today we're going to learn how to encode categorical data.
So the first thing that we're going to do
is that we are going to explain why we need to do this.
So I'm going to go to Google Sheets here to find my data set.
And in this data sets we can see that we have two categorical variables.
We have the country variable here and the purchase variable.
These two variables are categorical variables
because simply they contain categories. Here
the country contains three categories.
It's France, Spain and Germany.
And the purchase variable contains two categories yes and no.
So that's why they're called categorical variables.
And now you can guess
that since machine learning models are based on mathematical equations,
you can intuitively understand that it would cause some problem
if we keep the text here and the categorical variables
in the equations, because we would only want numbers in the equations.
So that's why we need to encode the categorical variables,
that is, to encode the text that we have here into numbers.
Okay.
So in this
tutorial we are going to encode these two variables country and purchased.
And you're going to see that the technique is quite different.
So here we are.
We're going to use something very practical in R
that is the factor function.
And the factor function
will transform your categorical variables into numeric categories.
But it will see the variable as factors.
And you will be able to choose the labels of those factors.
And due to the specific reason,
we will need to create three columns for each category.
We will just transform the country column into a column of factors,
and we are going to specify what the factors are.
So let's do this.
Let's start by encoding the country column.
So we just need to take the column country.
And to do this we type data set dollar country
equals.
And then we use the factor function factor.
And in factor we are going to specify three things.
So let's have a look at the factor function.
To do this we click on F1 okay.
So this contains some info about the factor function.
Let's look at the arguments.
The first argument is the data that we want to transform into factor.
So this is of course going to be our.
Country column from our data set okay.
Then the second parameter is levels.
And that's the names of the categories in our country column.
So let's add here levels equals.
So are we going to write the vector of levels here c parenthesis.
And then we will input here our three categories.
So that's France.
Spain and Germany.
All right.
So by the way C here is a vector and r.
So by creating this c France Spain Germany
we are creating a vector of three elements
that is France Spain and Germany
okay.
And the last arguments
that we need to input is labels because we want to choose which number
we want to give to France, to Spain and to Germany.
So we're going to add here labels.
And then since we transform our country categorical variable
into factors, we don't really care what numbers to use.
So let's use one for France,
two for Spain and three for Germany.
So this has nothing to do with my preference.
It's not because I'm French that I choose one.
For France, it is absolutely none order relative.
So it's just by default that I use one, two, three.
Okay. So that's it.
We just I just forgot one parenthesis and I think this should disappear. Yes.
Good.
Okay, so the encoding of the country categorical variable is ready.
It's actually more simple than in Python.
So before selecting and executing this let's look at our data set.
Our data set contains our country written in text.
And now let's select this
execute.
And now let's look at our data set.
Perfect.
Our country contains our encoded variable.
And as you can see one is France, two is Spain and three is Germany.
Okay. So that's all good.
And now we need to do the same for the purchase column.
And it's exactly the same.
We're going to use the factor function.
We are going to transform the text categories into numerical labels.
So let's do it right now we're going to copy this paste it here.
Here we're going to replace country by purchased.
All right.
Same for here.
Here we need to change the levels because there are new categories.
So the levels are going to be no.
And yes okay.
And now of course we need to change the labels.
So we're going to put zero for no
and one for yes I think that's what actually makes sense.
And parentheses okay. And that's ready.
Let's look at the data set. The data set contains no.
And yes in the purchase column.
And now if I select this and execute
the data set contains now zero and one.
Perfect.
We're all good.
We, we encoded our categorical data in R, and now you know everything
that there is to know about encoding categorical data in Python and R.
So congratulations.
We almost did the most difficult part we are approaching soon.
The fun and exciting part about making models.
So we are almost there.
Hang on for 1 or 2 more tutorials and this is going to get
very exciting.