Hello everyone and welcome to part two of the solutions lecture for the decision trees and ran them
for this project.
Let's go ahead and jump back to the notebook where we left off last time.
OK.
Here I am back at the notebook.
Last time we finished the exploratory data analysis were some visualizations and we're going to go ahead
and move on to setting up the data.
If you go ahead and check out the loans the info again you'll notice that we have a categorical column
we have to deal with.
And that was the purpose column that we previously plotted out with this visualization.
Here let's go and use PD or get dummies in order to transform that into some dummy variables.
I'm going to show you how you can do this with PD Dockett dummies and Pandurs.
First thing to do is create a list of one element containing the string purpose.
We're going to call that underscore feet's.
And the reason we're doing it this way is just to show you how you could basically expand this to multiple
categorical columns.
Now go ahead and use PD Dockett dummies.
Loan's columns equals cat underscore feet's and drop underscore.
First it was true to create a fixed larger data frame that has new feature columns with dummy variables
and will set this equal to final data.
So I don't see that will say final underscore data is equal to PD get dummys and the we're going to
pass in loan's as our data frame will say the columns we want to convert to dummy variables is underscore
feet's and that's going to be the list of columns.
Me to make sure this ran.
So we have cat underscore feet's here and then we're going to say drop first equals true to avoid any
multi-collinearity issues.
Run that and now we have our final data.
Let's go ahead and take a look at it with info and you'll notice that we have several purpose columns
credit card debt consolidation educational home improvement and if we go out and check out the head
of the data frame and we scroll all the way to the right one notice we basically have a bunch of zeros
and ones here for each of those purposes.
That's exactly what we want.
Now we can go ahead and train to split this data and get to use from S-K learn dot cross-validation
import train test split and then call train to split shift tab so I can see the documentation here scroll
down and copy and paste the slide to save myself some typing
and then I'm going to go ahead and set test size to point 3 in random state to 101.
So your results can match mine if you want to follow along.
And we need to specify where x is equal to and what y is equal to Y is just what we're trying to predict
.
In this case it is not fully paid column so not fully paid.
And X is everything else so that's final data and we just drop the not fully paid column along.
X is equal to one since it's a column.
It's going to run this.
Make sure it works great.
Now we can go ahead and train a decision tree model.
So we're going to train a decision tree just a single one so we can say for mid-scale learned that tree
import decision tree classifier are going to go ahead and create instance of a decision tree classifier
.
So I'll say the tree is equal to decision tree classifier and I can just use tab to help myself autocomplete
some of this and I'm going to fit this to the data so the tree fits on the X train and the white train
data PERFECT AND else go ahead and get some predictions and evaluate our decision tree so I can create
predictions as always just by calling the Predict method and then passing in the test features.
In this case that's X underscore a test and then I want to go ahead and maybe make a confusion matrix
and classification course all say for mid-scale learn that metrics in poor classification report and
then a confusion matrix.
Or go ahead and print out my classification report.
It uses in tab here with wide test and my predictions.
Go ahead and run this and it looks like we have precision recall are around 75 percent hovering around
there.
I can go ahead and check out the confusion matrix as well.
So confusion matrix y tests and then predictions.
And don't worry if your results didn't match exactly what the output was.
It kind of the pens on how the Ranleigh splits happened for the train to split and later on for your
random forest model.
That will also take into account as far as the actual run the splits of the trees themselves.
All right.
So we have classification report reporting that we have around 74 75 percent precision recall depending
on what topics or what classes we're actually looking at 0 versus 1.
Let's go ahead and turn to a random force model to compare the two.
We'll go ahead and create an instance of a random force classifier class and fit it toward training
data from the previous that so random force is from the ensemble family because it's an ensemble of
the entries are going to say import random forest classifier RAFC and then go ahead and tab autocomplete
that we can select the number of estimators go ahead and selects maybe something around 300.
Since our data is quite large.
But again this is something and play around with at a certain point the number of estimators increasing
won't help you anymore you'll just kind of hover around a particular accuracy score that a low number
can be improved.
We'll go ahead and say RAFC thought fit.
And then we'll go ahead and fit this to or X train and y train values.
And this may take some time.
It takes longer if you have more estimators So keep that in mind.
Now let's go ahead and get some predictions and evaluate our random force model.
I'm going to go ahead and say predictions is equal to our F.C. predicts off of x.
Test.
Go ahead and run that and then we'll create a classification report from the results and I can actually
just copy and paste this from this slide since we are also calling predictions.
Here we overwrote predictions.
I'm going to go out and run that and see what we get.
OK.
Now let's go in and check out the confusion matrix by essentially running the exact same line but instead
of classification port we will get confusion matrix.
Just a tab autocomplete there.
All right.
So notice now the precision recall and EF 1 score for certain classes has improved and overall on average
divided by total it's improved.
But the recall for instance for Class 1 here it's 0.02.
And if you go back to a single decision tree it's 0.2 3.
So the question now is what performs better the random forest or the decision tree.
Well it really depends on what metric you're trying to optimize for.
And I really want you to notice here the recall for each class for the models.
Neither did very well but the decision single decision tree did better on Class 1 for recall than our
Random forest here which only got point 0 2.
And likewise for EF 1 score being at point 0 4 or class 1 versus point 2 2 for Class 1 on the decision
tree.
So it really depends on what the costs associated with each of these calls is.
Overall if you average it the random force model that better.
But if you look at particular spots it did much worse.
It really depends on your data.
And that's the kind of thing where you'll need your business domain experience.
Third Circle of that Venn diagram to actually understand what's the best model here.
All right.
You may go ahead and explore this further by trying to engineer some more features to try to help your
precision and recall values for certain classes.
All right.
Hope you enjoyed this lecture and the solutions.
Thanks.
I'll see you at the next one