Hello and welcome to this art tutorial.
Here we are at the final round of regression
with our final regression model random Forest Regression.
In the previous section we saw the decision tree regression model.
So if now the decision tree regression doesn't have any secret for you,
then you will perfectly understand random forest regression
because random forest is just a team of decision trees,
each one making some prediction of your dependent variable
and the ultimate prediction of the random forest itself is simply
the average of the different predictions of all the different trees in the forest.
And actually, at the end of the previous section
about decision trees, I asked you an enigma.
The enigma was knowing the results we got with one tree, what would be the result?
With ten trees or 100 trees or 500 trees
in terms of visualization and in term of prediction.
So I hope that after watching the intuition tutorial made by Kirill,
you actually asked yourself this question and tried to predict
what's going to happen here with random forest regression.
So let's find out about that.
We are going to build a random forest regression model and see what happens.
So let's do it.
We're going to start by selecting the right folder as a working directory.
So it's in part two regression.
And here is the final regression model.
We were building random forest regression.
So let's go inside.
And that's the right folder we want to set
is working directory with the position salary CSV file.
So let's click on this more button and set as working directory.
All good.
And now let's take our regression template to build this model efficiently.
So we're actually going to take everything from here to the bottom.
But we will only include this code section to visualize the regression
model results.
Because you understood that the decision tree regression
model is a non continuous regression model.
And since random forest is a combination of decision trees
then it's a combination of non continuous regression model.
And intuitively we understand we can guess that the random forest
regression model is not going to be continuous either.
So since this code doesn't work for non continuous regression model
we will actually use this one.
That works perfectly for it.
So I'm going to copy this paste that here and remove this section.
That is non appropriate for non continuous regression models.
Here we go.
And now the template is ready.
Let's change the basics.
Let's replace here regression model by random forest
regression.
Visualizing the random forest regression results
and fitting random forest regression to our dataset okay great.
So now let's build the model which is in this section here.
So let's remove this.
And as usual we're going to import the right library for the job.
And then use a function to build our random forest regressor.
So the package we are going to import is called random forest.
So for those of you who don't have the package installed
in your packages here, well you can check it out.
Mine is already installed because I used it before,
but I'm going to write this line here for those of you who need to install it.
So install dot packages,
parenthesis and in quotes random.
So no capital R but then capital F.
Oh right. All right random forest.
And so I'm not going to install it because mine is already installed.
So I'm going to put that in comment.
But if you want to install it you just need to select this line as
I just did and press Command and Control plus enter to execute it.
And this will install the package properly.
But here I'm going to put in command by pressing command plus shift plus C.
Here we go.
And now when we have to do is to add this you know library
random forest to actually automatically select
the box here to import automatically the random forest package
when we execute the whole code or the section.
So that's important.
And now time to build the regressor.
So let's do it.
We're going to call regressor regressor as usual to keep things simple and equals.
And now the function that we're going to use is also
random forest written the same.
And so now let's add some parenthesis.
And now let's press F1 to have a look at the arguments okay.
So the arguments are here.
And the first argument is data.
But as you can see it specifies that it's an optional dataframe.
And we could use this argument to build our regressor.
But we're going to use the main arguments to specify, you know, the independent
variables on one side and the dependent variable and another side.
And to do this we're going to use these two arguments x and y.
So x will contain the matrix and features that is the independent variables.
And y will contain the dependent variable vector that is the salary column.
So let's first input these two arguments.
So the first argument is x equals.
And so we have several ways to take our independent variables.
So one of the way is to take our data set here.
And then choose the right columns of the independent variables.
And you know our data set is composed of two columns.
The first column indexed by one which is the independent variable column.
And the second column indexed by two which is our dependent variable column.
So here we need index one because we want to take the independent variable.
All right.
Now next argument.
The next argument is y the dependent variable vector.
And now as you can see y is expected to be a response vector.
It's actually a vector.
And here it expected to have a data frame.
So by using this one index into brackets here I actually import a data frame.
But here to get a vector I actually need to use another trick,
another technique which is in know to use a dollar sign.
And then the name of the column, which is of course salary.
And that will give me a vector.