And now we actually need to input a third argument.
Can you guess what that is?
For those of you
who followed the Python tutorial, well, you will guess what it's going to be.
It's actually going to be.
And tree the number of trees in the forest.
Well, of course we're building a random forest, so it's actually a lot better
if we can choose the number of trees that we build in our forest.
And it's even better considering the fact that we're going to play around
with different number of trees.
That is, we're going to start with ten trees with a forest of ten trees.
And then, you know, we'll try with a lot more than ten trees, like 100 trees
or 300 trees or 500 trees.
So that's where we're going to input the third argument and tree.
And we're going to start with ten trees.
All right. So let's start with this.
And that's all the arguments we need to build a random forest.
We only need independent variables the dependent variable and the number of
trees.
And that will already make a robust random forest regression model.
And then we will make it even more robust by adding more trees in the forest.
But before we continue, let's set the random factors
to something fixed so that we all get the same results.
So, you know, in Python we used a random state parameter equal to zero.
Here we can do the same on R by using the set
dot seed function.
And then in this function we actually input a seed.
And you know we can use whatever seed we want.
In Python we usually take zero 42 and an oh what we like to do
is you know, to take either one two 3 or 1, two, three, four.
So let's use the seed to all get the same result.
And that's what make this tutorial easier to follow.
If you're coding at the same time.
So now we're all good.
We're actually all good with the whole code.
We don't have anything to replace.
The only thing that we'll do now is to, you know,
try several random forests with several number of trees.
And look at the visualization results and look at the prediction
to see if we're getting close to the supposed 160
K preview salary of our new employee that is about to be hired.
So let's do it.
Let's execute the sections one by one.
So let's import the data set first.
Here we go.
Data set will import it.
We make sure we have our two columns the independent variable level
and the dependent variable salary. Perfect.
Now no need to split the data set into the training set and the test set.
No need to apply feature scaling.
And now time to create our first random forest.
So let's do this.
Let's execute this code section here.
And here it is. Random forest.
Well created. Perfect.
So now it's time to have fun.
Would you like to visualize the result?
First or getting the prediction?
Well, first let's maybe visualize the results
because we want to make sure we have the right model
and we want to validate it because we will try several number of trees.
Here we are starting with ten trees.
So we want to see if it looks like a correct model.
So I'm going to execute this section.
Here we go. And let's see what will get.
Okay.
So first of all this looks fine.
We don't seem to have any problem here.
The only thing that we can improve very quickly is actually, you know,
those straight lines here.
There are supposed to be vertical.
And to get a better representation of this,
we just need to increase the resolution as we did for decision tree regression.
So let's add 0.01. That will be sufficient.
And let's re-execute this.
And now much better.
It almost looks like it's
some vertical straight lines representing better than some continuity.
And so now what can we say.
Let's zoom on this plot to have a better look.
And now listen to it.
Okay, so the answer to the enigma that I asked you in the previous section
and that I was asking you again in this tutorial,
is that we simply get more steps in the stairs
by having several decision trees instead of one decision tree.
We have a lot more steps in the stairs than what we had with one decision tree,
and therefore we have a lot more of splits of the whole range of levels,
and therefore a lot more intervals of the different levels.
So each straight horizontal line here separated by these vertical lines
or one interval that is one split.
And the fact that we get more steps in the stairs is actually quite intuitive
because, you know, if we get, for example, this prediction here for the 6.5 level,
well, what happened for this prediction is that we had ten trees voting
on which step the salary of the 6.5 level position would be.
And then the random forest took the average of all the different predictions
of the salary of the 6.5 level made by all the different trees in the forest.
And for example, if we take the fourth position level, ten votes were made.
Each of these ten votes correspond to one prediction
of the level for salary made by each one of those ten trees.
And then the random forest took the average of these ten predictions.
And this average is nothing else than the prediction of the level
for salary made by the random forest itself.
And so we get more steps, because simply the whole range of levels
is splitting into more intervals.
And that is because the random forest is calculating many different
averages of its decision trees predictions in each of these intervals.
So that's what happened. It's quite intuitive.
However, there is something important to point out here
is that if we add a lot more trees in our random forest,
well, it doesn't mean we'll get a lot more steps in the stairs,
because the more you add some trees, the more
the average of the different predictions made by the trees is converging
to the same average.
You know, this is based on the same technique entropy and information gain.
So the more you add trees, the more the average of these votes
will converge to the same ultimate average
and therefore it will converge to some certain shape of stairs here.
So that's important to visualize this as well.
And now since we have our intuition of the visualization of the random forest
regression in 1D, let's see what happens with the prediction.