In the last video, we saw how to create Crinum forest classifier in Python.
And if you remember, we provided several parameters.
Two beloved Regnum for us.
We provided a number of trees we want, and we also provided the default value of the number of variables
we want to consider.
For each three of the random forest.
Their default value we used was a square root of the number of variables which comes out to be around
four and five.
So if I want to change the value of this hyper barometer's, I have to read on my whole code and then
knock down the values of this accuracy or any other variable on which I am evaluating my model performance.
So crying.
Oh, different values of such hyper parameters will be a hectic task for us.
To overcome this, Escalon provides us another function, which is great, sir S.V..
And it's a CV, you can provide values of all the hyper parameters you want to consider for your more
than.
Then by 10:00, will automatically create model for each of such hyper parameter.
And after that, you can select the model which provides the best accuracy score or any other evaluation
parameter.
So let's look at the documentation of research.
We have to import grid search from as Skillern Todmorden selection.
And then this are the parameters of grid search.
The first one here is the estimated.
Here you have to specify the model on which you want to change the hyper parameters.
Since we want to give multiple Hyppolite, I went to this photo at Random Forest.
So we will provide Regnum for us as estimated here.
The next is the Padam under the sort of grid.
This is an very important parameter here.
You can mention the parameters and their values in the form of dictionary and grid search will automatically
pick those parameters and create models using those hyper about their values.
The next phase is scoring.
So by default, your grid search.
Use a cross-validation approach.
That means that it will automatically take a segment of your data off your training data and calculate
calculate the performance of your Morden using that data.
So here you can provide that is scoring my word on how you want to select your final model.
Since we are building a classification model, we will stick with accuracy as our scoring method.
All other parameters are not important.
If you want.
You can go through each parameter as well.
So let's go back and look at the code.
First, we are creating our own name for this classifier.
So we are just writing their name for this classifier.
And here we are giving our hyper barometer as number of trees to be 250.
And there are no Misato 42 so that we can reproduce the same results later on.
Next, few remember, we have two more hyper barometers for our random forest there, days max features
and minimum sample split.
Max feature is the number of variables we want to consider while creating each individual tree.
By default, this value is a square root of the number of variables which come so to be fought in our
case.
And here we want to crane our model for maximum feature value of four, five, six, seven, up to 10.
The second hyper parameter we can change is minimum, simple split.
So if you remember in Crete, there were three stopping our nation.
That is maximum depth.
The minimum number of observations required at internal load and minimum number of observations required
at the leaf node.
So this one here, minimum sample is split as the minimum number of observations required at 10 and
then unknown.
And here also we want to try out three values of minimum simple split, so we are giving two, three
and 10.
And if you see this bad grade is their dictionary.
And if you remember, dictionaries consist of key value pair.
So although we're hyper parameters will be keys and our values will be in the form of list of those
hyper parameters.
We want to give.
So if you want to add any other variable to try out different values of their TypePad parameter, you
can just put a comma and give a variable name here.
So, for example, if I want to try out the end estimated value of two fifty five hundred and thousand,
I can just provide an estimate that here.
And I can provide values of two fifty five hundred and thousand in the form of the list of this key
value pair.
So let's run this.
So as I told you earlier, for grid search, you have to wait for the classifier, our classifier is
this random forest classifier that we created earlier than we have to provide the dictionary of all
the hyper parameters.
Well, do we want to try?
So we have created this dictionary value.
That's why we can radically provide under school grade.
And then this end job minus one is to operate our model at the full efficiency of our laptop.
Then C.V is for cross-validation.
So we are giving it as five.
And we want to escoto or model using accuracy
just like any other model.
We are saving this model.
And another object that is good on that score to search.
So let's run this.
But before that, we have important grid search seewhy.
It's important that and create this group set to object.
And now, just like with any other classifier or regression object, you can do it extra and then by
trying to turn to grid search.
Since we are building so many models using this single come on.
It will take some time.
The number of models we are creating.
It's the product of the number of individual hyper barometers we are providing.
So here for maximum feature of four.
We are building three model with minimum simple split off to three and then then again for maximum feature
of five.
We are building, again, three models, similarly for six.
We are building three models.
And for each of these values, we are building three, three, three models.
So in total, we are building seven in two three models in this single.
Come on.
So you can see there is no need to manually enter hyper parameters each time.
You can just provide them as a key value pair in their dictionary and you can run a grid search over
all the parameters and their values.
So we have green node 21 models.
So this grid search here contained information of 21 models that we have created using this hyper parameter
values.
Now using cross-validation, we have also calculated that escort's of this.
Twenty one more chance now to get the information of the best model that we have in this 21 model.
You can call this best underscore bedtime's underscore attribute of grid search.
So grid search comes with different attributes.
And this is one of their tribute which will give you information of the best model.
So just run this.
You can see we are getting our best model when our maximum feature value is seven and minimum sample
displayed is three feet.
Remember, we use seven values of maximum feature and three values of minimum sample displayed.
So out of the seven values of maximum feature, we are getting our best reserved at maximum feature
equal to seven and out of this three values of minimum Symbolistic.
We are getting our best result at minimum.
Sample split as three.
Now, to get this more than from 021 model, we can use best underscore estimates, underscore attribute
of grid search.
So this attribute here contains the information of our best model.
So we are just creating another variable to get this information.
And later on, we can use this C.V AALDEF underscore to see a lift variable to credit total values of
best.
So we will run this.
No, no word S.V. R.F. underscore to see a left variable.
Contains the model for which maximum featured is equal to seven and minimum, but split is equal to
three.
And this is the best model out of the 21 models we have created using it set.
Now, to get the accuracy score on our test data, we can medically call a curious end of school function
and we can use the Dort critic method to credit the values of why test.
So let's run this.
The accuracy is scored for our best model.
All of this different values of hyper barometer's as zero one six three and you can also draw confusion,
the matrix using the same technique.
So this is just an example on how to pass multiple values of hyper parameter and get the best result
out of all the hype at parameters values.
You can write on your own, try providing more.
I put parameters and their values in this part ended and tried to get the accuracy scored more than
this accuracy score.
So with grid search, there is no need to rerun your model again and again, you can use grid search
to train multiple more than a single line of code.
Thank you.