Hello everyone and welcome to the entries in random force with Python lecture.
This lecture will show you how to use sikat learn in Python to create the entry models and random forest
models.
Let's go ahead and jump to the notebook to get started.
All right.
As a quick note you can find this notebook is under the machine learning sections folder.
This isn't treason random force.
And then you'll see three notebooks as well.
Two data sets.
We're going to be walking through that decision trees in around the force in Python notebook.
And then you have your project notebook and your solutions to that project notebook will first start
off by just working through this lecture notebook.
And if you click on that note book should look like this.
We're going to basically be going through this notebook explore the data just a little bit focusing
more on the training of the decision trees and the random force as well as seeing how we can make a
visualization of a decision tree as a quick note.
I also have a link here in the electronic book to a blog post explaining the general logic that decision
trees and random forests which you can also check out in case the previous theory lecture wasn't quite
enough for you.
Let's go ahead and walk through this notebook.
I'm going to go ahead and just create a new notebook to get started.
OK the first thing I'm going to do is import my library's First I'll start off with pandas and pi as
an MP and then my visualization libraries which is always is map plot lib plot as Piel team and seaborne
as asinus.
And then since I'm using it Jupiter note notebook I go ahead and say map it in line.
Now let's go ahead and grab the data.
The data we're going to be working with is the first data set so we can go ahead and start typing in
K Y and click tab and it will auto complete.
And then we can check out the head of this data set.
So this dataset essentially represents a number of patients who had facias which is a spinal condition
and then they had an operation
and the operation was a corrective spinal surgery and the state of frame basically represents whether
or not the officers condition was absent or present after the operation.
And the age is the age of the person in months.
So this is data on children.
The number is a number of vertebrae involved in the operation and the start was the number of the first
or top most or vertebrae that was operated on.
Again you don't need to know too much about the medical condition and what's actually going on here
since we're really just going to be dealing with all these columns as numbers and the offices as essentially
a target absent or present.
Let's go ahead and move on with a really simple exploratory data analysis since the status is actually
quite small and we can check that by doing the info method.
You'll notice it is actually just 81 entries so quite a small dataset meaning I could just call S and
S pair plots.
They make sure it was imported as S and S appear not as an pair plots.
Passing my data frame ZF and set Hue equal to the first column.
Now let's go ahead and run that.
And here we have our pear plot where we can see how the columns are related to each other as well as
the distribution.
Let's move on by going straight to the machine learning modeling side of things like training and testing
split our data.
So I'll say from S-K learn cross-validation in poor train test split I want to set my X data to everything
but the target.
So it's going to be DSF drop and the target is Infosys because basically we're trying to predict whether
patient after a certain number of months after their surgery.
Are they healed or not.
Is the condition still present.
And I'm going to go ahead and say Axis 1 and then we'll go ahead and say the target is the first column
then we call train split as always and grab the boiler plate code over here or we can just copy and
paste going to copy paste that and then I'll set my test size two point three and we can just go ahead
and use the default random state.
Or we can pass and 101 whatever you prefer.
Let's go ahead and start off by just training a single decision tree in order to do that with sikat
learn you'll say from S-K learn that tree import.
And then you call decision tree classifier for classification and then we will create a model called
the Tree of the decision tree classifier.
Go ahead and instantiate it.
And just like all our other models of sikat learn well go ahead and fit this model to the training data
.
So X train y train and then I'll show you the various parameters the default ones are fine for now for
our decision tree classifier.
We'll go ahead and evaluate how well our decision tree was able to predict based off of these columns
age number and store whether or not careful is still present in this child.
We'll say predictions is equal to the tree predict off the test set and or score a test and then we
will say from S-K learn that metrics imports and we'll go and import classification report and the confusion
matrix and then we'll go ahead and just print those out.
So we'll call Prince confusion matrix off our test data and off our predictions and we'll do the same
thing except instead of confusion matrix it will be the classification report.
Now we can actually tab autocomplete here as well.
And let's go ahead and put in a new line between these two.
Just for readability.
All right.
So it actually looks like we're doing pretty good.
So some aspects we are mislabelling about six of these from the test set which gives us about an average
total of somewhere between 70 all the way up to 87 percent of the pending on what metric you're qualifying
either precision recall or accuracy.
All right.
Now let's see how these results compare to a random forced model.
Well we'll go ahead and do is safe from Eskdale learn ensemble in poor random forest classifier.
And the reason it random for us classifiers under ensemble instead of tree is because we remember ran
the forest from the theory lecture are a bunch of essentially decision trees and it's an ensemble of
them with certain caveats such as the splits being chosen randomly etc..
We're going to go ahead and create a random forest classifier model.
We'll say it's RAFC random forest classifier and we'll go ahead and say the number of estimators is
equal to 200.
And you can go ahead and play around with this estimator number see what works better for you or what
works worse 200 should be fine.
Since our data sets are actually quite small that's actually probably overkill but we'll go ahead just
leave it at that to reflect a normal situation then we'll say our FC that fits in we'll pass an X underscore
train.
Why underscore train.
And then we'll go ahead and begin to fit our data will say Aref see are the actions or pred is equal
to RAFC that predicts off of the testator and then we can go ahead and print the confusion matrix and
the classification report actually is going to grab this up here.
Copy that and paste it here.
And instead of saying predictions I will replace that with our RAFC underscore Fred.
And here as well or replace this with RAFC underscore pred it's going to run this and compare the results
to our decision tree.
All right.
Just looking at the results here we get about 88 recision 84 percent recall 86 percent one score essentially
a measure of accuracy.
And here we're looking at 87 76 and 80.
So it's a little hard to tell which performed better because it really depends on what you value whether
you're Valley precision or recall and if you value it for being absent or present.
So it's probably more important to realize that it's still present versus it's absent.
But again it really depends on the situation and the patient itself and what costs are associated with
those decisions.
But it's also useful to compare is the confusion matrix here.
So we can see the confusion matrix.
We have 1 in 5 being mislabeled here and we also have one in three being mislabeled here.
So overall it looks like the random force data a little better.
And you'll notice that as data sets gets larger and larger the random force will pretty much always
do better than a single decision tree here because they just that's so small the random force wasn't
able to outshine the decision tree by as much.
Then the other issue with that as well is if we take a look at the catharsis data set again.
So we'll go ahead and just look at the if we take a look at the actual Infosys column and then we take
a look at the value accounts.
Notice that we have a lot more cases of it being absent versus present.
So already our label in our dataset is unbalanced there's a lot more instances of absent versus present
and that could also affect models as you go along.
So keep that in mind as you're comparing decision tree in this case versus a random forest classifier
But overall and pretty much most cases of larger data sets a random force classifier will outshine a
single decision tree.
Now one thing I wanted to know is the use of we go back to the actual lecture note book here and notice
here in the middle the figure there's actually a quick aside to visualizing your tree sikat learn has
some built in visualization capabilities for decision trees and you actually use it often because you'll
probably just be using random force which are much harder to visualize in this sort of manner because
you have hundreds of trees versus just one decision tree but you can go ahead and explore this code
here in order to see the visualization of decision tree.
There's a couple of things to know.
One it relies on a library called Pied-Bot Pied-Bot does not come included with an anaconda distribution
or the normal Python distribution so you'll have to go back to your command line or terminal and type
in.
Install Piedad.
That's PKP space install and then Pied-Bot.
P y t.
That will allow you to run this code.
And the second thing to know is that this very last line graph that create G will also not run unless
you have the graph.
This library the graph this library is an additional library that will actually be able to take this
graph object and create a picture from it.
And this graph library is actually this graph.
This library is not a Python library.
It's another program essentially altogether.
So you would have to download that separately.
Which is why we actually won't go through this since it's quite a tedious task to download it and install
and set up the paths and you won't really be using a single decision tree throughout the rest of the
Course as far as visualization for it.
So we'll go ahead and just show you the link on where you can install it.
You can go ahead and just google search graph is that's going to be g r a Ph.
V easy or just go to graph this org.
And this is graph visualization software.
So it's a completely separate software from Python.
But you have to download in order to see that visualization.
So as an option if you want to see those visualizations you can come to this Web site.
Scroll down to see download and you'll click on the download link.
It should show you the packages they have download links for Mac and Windows on a Windows computer.
Keep in mind that you'll have to set up the path variable yourself.
It's no longer automatic.
Again like I mentioned it's quite tedious just for that simple visualization which is why we're leaving
this as completely optional for you to undertake if you want to.
And you can go ahead and post the Q&A forums if you have more questions about this.
Well go ahead and end their lecture here.
Again you can check out the full notebook of decision trees and ran of course if you want to reference
the code for the visualization.
What I want you to more importantly get out of all of this is the ability to just call decision tree
from the S-K learn that tree and compare it to a random forest calling that from SK learned the ensemble
random force is an extremely powerful tool when it comes to machine learning algorithms.
And a lot of times it's a data scientist first quick choice for recreating a very fast classification
model as far as just trying to see what's kind of a baseline accuracy or precision or recall you can
get a model before you start kind of playing around for other other models or tuning stuff.
A lot of times you'll just quickly slap on a random force classifier on it or in the force regress or
check it out and see how it does.
Just so you can get an idea of what's actually possible with the data.
All right.
I hope you enjoyed the lecture.
Coming up next is your project overview lecture.
Thanks everyone and I'll see you there.