In this lecture, we will learn how to do pruning in Python.
No debt in Python.
We can only do three pruning and we will not be able to do both.
Pruning as Python.
Do not support forced pruning.
We have discussed these how to a strong growth or for Greece.
We discussed three ways.
First one was to pass the maximum depth of forward Crete.
The second way was to pass the minimum samples that required it and done a load to do this thing.
And the third way was to provide the minimum sample required in the leaf node.
We will use these three ways to control our Greek growth in Python.
I hope you remembered how we used Max depth parameter of our Engracia object to give the maximum number
of levels in our tree.
So we will first try this method.
The code will remain same for us.
We are creating a regression tree object.
We are naming it Regg Cree one.
Then we are creating this object with the Crete Library of Eskinder.
And here as a parameter, I am passing max depth equal to three.
If you want to know about all the parameters that you can pass.
You just have to enter shift tab and it will give you all the parameters that you can provide in this
object.
If you head shift tab two times, it will open a larger window and if you hit ship tab three times,
it will open the help window in the bottom part of your screen.
You can go through all of these parameters on your own.
But we will be on there discussing the important ones, the forces maximum depth here.
You can define the maximum levels you want in your tree.
The second step will be to fit a what, ekstrand wide trend data and do this figuration tree object.
Then in this three steps, we will create the DOT data and cloud the word regression tree.
Using that DOT data, I have added to more parameters here.
So if you remember earlier we were getting our columns as X zero X one.
So here, if you mention feature name as parameter and then if you mention the columns of your X data,
then in the final three you will get variable names, insert of X one or X two or X3.
The next parameter I have added is filled.
It will fill Galut according to the value of that bucket.
So it's just like conditional formatting in Excel.
If you know what this conditional formatting of food parameter equate to grow will sort of do a conditional
formatting on your three buckets.
So let's run this on.
So this is our tree.
You can see that the maximum depth is three here as we have provided the condition on maximum depth.
This is the level one.
This is lower.
You do.
And this is level three.
If you see inside this small cells, first we have the variable name and then we have the condition.
So what first a split is on, but your variable and if budget is less than thirty seven thousand nine
hundred, then we are falling.
This branch and if budget is greater than tardies own thousand nine hundred, we are following this
branch.
We have the MASC when you add this bucket of over KRI.
Then we have the total sample size at this bucket.
So since our total sample size of the crane data is four hundred and four.
That's why we are getting sample is four hundred and four.
And the last is the average value of our collection variable in this bucket.
So if you notice, the colors are changing.
According to this collection value here, the collection value is hundred thousand.
That's why the color of this cell is darkest.
And here are the main collection value is twenty eight thousand eight hundred.
That's why the color is latest.
You can see all this information in all the cells and you can follow these branches and cells to find
the final condition of your leaf.
Note, for example, for this leaf note or condition, this budget is less than thirty seven thousand
nine hundred eighty two views is more than four 40000.
On the left side is always for crew and the right side is always for false.
So that's why Krilov view should be greater than for 40000 for this cell.
And then of the dedicated rating is greater than nine point one six.
We will get this book.
So there are two observations which are satisfying this.
All three conditions and the average value of our collection data for these two observations is underclothing.
This is the first day of pruning or tree we have used maximum depth as a barometer.
The next we will discuss was to give minimal observations required at Internal A.
That means that if you look at here, here, our sample size is 24 and still we are splitting it into
two parts.
So if I mention my minimum sample is split as 25.
That is 13 would not take place only if the sample size is more than the minimum sample split.
Then only four that is splitting will takes place.
So for the second way, I will give minimum sample split of 40.
So I'm seeing that for every splitting minimum 40 sample size required in that bucket.
Here I am creating Rectory to object and the rest of the boards are the same.
I will run this.
So this is our.
You can see that the maximum depth here is NorTech Willow Tree.
It is much larger than you see for every splitting node.
We have a sample size greater than 40.
So this is this is another way of running your tree.
The next way is to provide the minimum number of samples you need in your leaf nodes.
So all this and nodes are your leaf node.
And you can see there are some leaves where the sample size one as well.
So you can limit your tree growth by giving value of this minimum sample required that each leaf node.
To do that, you can pass the parameter means simple leaf.
And as always, you can always shift up to getting details of all the parameters available within the
Earth.
Be sure in creating this set object.
So here I will give my minimum some belief as friendly fire.
And I will run this code again.
You can see this is my new tree with condition on leaf notes, and you can see that all my leaf notes
contain more than or equal to 25 samples.
You can apply multiple conditions as well.
So with this, I can also apply max depth.
See, I wouldn't give Meng's step forward.
I can read on this.
Here you can see that I have pruned my tree and now my tree only have four layers.
You can combine all the three conditions as well.
So play around with these values and try to get the optimal tree.
You can also use your Ardie Square and MASC value to evaluate performance of this tree in the later
part of forecourts.
We will see how to automate this process, how to pass multiple values of this high but parameters and
how to select the best model.
All of all these kinds of models that we are going to create will do that after our classification tree.
Till then, you can cry the friend values of maximum depth, minimum, some belief and minimum symbolist
split to get the best model for your data.