Perfect.
And now you know what the next natural step is.
Well, it is, of course, to create a new object of this
random Forest Regressor class.
And we're going to call this object as usual regressor,
which will be equal to, well, you know, an instance of this class.
That's why I'm copying it and basing it here and adding some parentheses.
All right.
So this time do you think we have to input something in this class.
Well let's not fall into the trap of too much easiness.
Let's say remember that for decision tree regression
we actually did not input, you know, an essential parameter.
We just input a random state factor in order to fix the seeds
so that we can all have the same results displayed in the output.
But this time there's actually a parameter that is very important.
And that parameter is the number of trees.
So that's exactly what we're going to input here.
The name of that parameter is an underscore estimators.
We're going to set it equal to 1010 trees right ten estimators.
Each tree is an estimate okay.
And then as usual we're going to add the random underscore state parameter.
And once again we'll set that equal to zero so that we can fix the seeds
and get the same output displayed in our notebook.
Okay great.
And now final step.
You know this step by heart.
Now it is so obvious for you.
It is of course to fit the regressor object to the whole data set.
In other words,
that means we are training the regressor on the whole data set, right?
So here we have to add a dot and then fit applied
to x and y.
Perfect.
So and that's all we had to do you know for this random
forest regression implementation.
All the rest is the same as the decision tree.
So we can just execute all the cells here and observe comfortably in our chair
the final result. So let's have a look.
I just want to warn you again that it's not going to be pretty.
And that's of course for the exact same reason as decision trees.
A random forest regression model is way better adapted to high
dimensional data sets or, you know, data sets with multiple features,
which you will see in the final section of this part two regression,
when not only learning how to evaluate your regression models,
but also on how to select the best model for any data
set you know for a particular data set, you're working with all right.
So let's do this.
Let's execute each of these cells
starting with importing the libraries.
Then I'm not going to forget I almost forgot to upload the data set.
You know I was about to execute the cell, but that would have returned an error
because indeed the data set needs to be uploaded.
So let's upload it now. All right.
So as usual we have to go into our machine learning.
It is that folder. Wherever you put it on your machine.
Then forward to regression.
Then section nine and last section of this part
two random forest regression and Python.
And there we go. Position salaries.
This is still of course the same data set okay.
So that's all good.
Now we have the data sets.
And now we can import it inside the notebook.
Well actually inside our Python program.
And now we're going to
train the random forest regression model on the whole data set.
So let's do this.
And this will output the random Forest regressor model with all the parameters.
And at this stage the only parameter that I recommend to tune
is indeed that number of estimators which we chose to be equal to ten.
All right. And don't worry too much about the rest.
Now this will already gives you an excellent model.
And then now let's predict the final result
by, you know, just calling this predict message from our regressor object.
And the predict method just has to take as input.
Well that's position level number 6.5 which remember
you have to input in a double pair of square brackets.
Because the predict method expects a 2D array as its input.
Right. So that's very important for you to know.
But we saw this many times.
I'm sure it has become also very obvious for you.
So let's do this. Let's get this prediction.
And we get wow we get a pretty good prediction actually $167,000,
which is very close to, you know, that salary that this person
mentioned to earn in the previous company, which was 160 K.
So that's very, very good.
And now let's visualize the final result.
Oh I actually forgot to delete that output.
But that's fine.
Let's run this cell and we'll get that output again.
And here is the regression curve of the random forest regression model.
And of course it looks like a lot the one of the decision trees.
Although this time there are more steps of the stairs,
you know, remember if the decision tree regression model,
we had a step for each of the position level.
And here for example we have two steps between two position levels.
So that's of course because we have more trees this time
and therefore more splits of, you know, the features where you have
the same prediction, you know, the average of the predicted salary.
So it only makes sense that there are more steps.
All right. So congratulations.
That was your final regression model of part two.
You now have a complete
toolkit of regression models, which gives you a lot of different options
and solutions for your future data sets and future machine learning problems.
So I'm really happy for you that we built together this toolkit.
Make sure to use it the right way.
And now we will finish with this last section
to teach you how to use this regression toolkit the best way
you know by selecting the best model for any data set.
All right, so join me in this next section.
I can't wait to see you again.
And until then, enjoy machine learning.