Hello and welcome to this art tutorial.
We are finally getting to the final round of our classification
adventure in our machine learning journey.
Because today we're making the very last classifier, the random Forest classifier.
So let's start with the basics.
We'll go to our Machine Learning A-Z
folder to set the right folder as working directory.
So this right folder is in by three classification.
And here it is section 20 random forest classification.
That's the right folder.
And as I was telling you we are reaching the end of part
three classification because the next section will be dedicated to evaluating
classification models performance in order to improve the models.
And therefore this is our last classifier.
So let's go to this folder.
That's the folder we want to set its working directory.
Make sure that you have the social network add CSV file.
And if that's the case
click on this more button here and then set as working directory.
All good here.
And now let's go to our classification template to build
our random forest classifier with the best efficiency.
So we'll just take everything from here
to the bottom copy
and then we'll paste it here.
All right.
So I'll just give a quick reminder about what this template is doing.
So the first step is to import the data set.
Then we encode here the target feature as vector.
Because we're just doing this to specify to the classifier that our last column
purchased is a categorical variable with factor levels zero and one.
So we don't need to do it for all the classifiers.
But we do need to do it for some, like the Naive Bayes classifier as we saw.
So it's good to keep this code section in the template.
Then we split the data set into the training set and the test set.
And then in the last section
of the pre-processing phase we apply features getting to the data.
And we are only doing this because at the end of this template we make this
very cool graph for both the training set result and the test set results
that will plot the prediction regions and the prediction boundary.
Okay, so right now we need to change a few things in this template.
First, let's not forget to change the title of the plot here
will replace classifier by random
forest classification.
We'll copy this because we'll do the same in the test set results here.
It is perfect okay.
And now what we only need to do is to create our classifier here.
And then we'll be ready.
So I'll just space the same thing here.
Random forest classification to the training set.
And now let's create our classifier.
All right.
Let's do this. So it's very simple.
We're going to use a library that is called random forest.
So let's do this.
Let's first install the package with the command install that packages.
So that's for those of you who are using this package for the first time.
Then you won't have it installed on your packages.
This here.
As you can see, mine should be already here because I used it
several times.
Yes, there it is, random forest.
So if it's not the case for you then you need to install the package here.
And so in this install the packages
function you just need to input the name of the library in quotes.
So it's random forest.
Be careful with the capital F here and not capital R here.
All right. So that's what you need to import anyway.
That will install the package.
I won't do it because I already have mine installed.
But what you just need to do is to select this and press Command and Control.
Plus enter to execute.
And this will install the package without any issues.
All right.
So here I'll just press command plus shift plus C to make it as a comment.
But then what I need to do is to add the command here library
and then input the name of the Random Forest library.
Because as you can see here, it's not selected.
So I need to add this library random forest command to select it automatically,
especially if I want to make some automated scripts in the future.
So that's the thing to do that's very practical.
And now we're all good.
Let's create our classifier okay.
So as usual we'll start by creating the variable classifier.
That will be a random forest classifier itself.
And now we will use the random forest function to build our classifier.
So here I'll just take the function random forest.
And now let's see what arguments we need to input.
So we'll just go here and press F1 to get some info about this function.
So here we need to click here.
And here are some info about the Random Forest library and function.
So let's look at the arguments which are which we are interested in.
Okay. So the first argument is data.
We will need it.
As you can see it's an optional data frame.
So it's not an argument that we need to build a random forest classifier.
Same for subset and action.
We actually don't need the three first parameters
to build a random forest classifier.
However, we of course need the x and y arguments here, which as you can
guess, will be the matrix of features and the dependent variable vector.
Indeed, as you can see, x is a data frame or a matrix of predictors.
So that's pretty clear.
X is our matrix of features a matrix of predictors
which are independent variables age and estimated salary.
And then y is set to be a response vector.
So of course y is the dependent variable vector
that is the purchased column okay perfect.
And then the last argument
that we will need to build our random forest classifier is
of course the number of trees we want to have in the forest.
And this number of trees is given by this entry argument here,
which as you can see, is the number of trees to grow.
So to grow is a nice way of telling that the trees will learn from the data set.
How to make the predictions.
And basically this argument is the number of trees.
So remember in the Python tutorial we chose ten trees.
Well let's do the same here.
Let's pick N3 equals ten.
And that is actually everything we need to build our random forest classifier okay.
So let's do this.
Let's input the arguments.
Let's go back to R and here we are.