Welcome back.
In the last video we finished off saying the next classification model evaluation metric we're going
to use is a confusion matrix.
Now if you're confused about the name confusion matrix Don't worry it will make sense after we go through
it.
So a confusion matrix let's define it is a quick way to compare the labels a model predicts and the
actual labels it was supposed to predict now.
In essence
giving you an idea of where the model is getting confused.
Beautiful.
Now again if it's still confusing don't why let's have a look.
That's what we always do right.
We prefer to get hands on.
So from our skyline.
We're going to go from The Matrix module.
Import confusion matrix and now we can create a confusion matrix by making some predictions.
We've seen how to do this with the predict function we're going to do that on the test data set and
then we're going to pass confusion matrix function confusion matrix y tests are the true labels and
the prediction.
So this is the crux of what basically all evaluation metrics are doing right.
They're comparing the true labels with the predictions and giving us some insights on that comparison.
And so if we hit shift into 0 we're getting a ride back or does this even mean just for numbers.
Again this is probably another thing that is easier to understand when it's visualized.
One way to do so is visualize confusion matrix with PD cross tab.
So let's see it in action.
So we're gonna go PDA for panders cross tab which is going to compare two different things.
Why test.
We want to compare the Why test labels again just another comparison between the true labels and the
predictions.
Why test why parades and we're gonna pass at row names is actual labels call them names is can you guess.
If not that's fine.
This is short for call names and this will be row names so we don't need that essay.
This is going to be predicted labels.
Wonderful.
So what is going on here.
Well because we are comparing the true labels versus the predicted labels.
This is what our cross tab of the confusion matrix is showing us so where our predicted labels where
our model has predicted 0 1 and our actual labels.
So here the rows are the actual labels and the columns are the predicted labels.
So in our case where the actual label is 0 and the predicted label is 0 we have 22 examples and then
where the predicted label is 1 and the actual label is 1 we have 24 examples.
And now if we total all of these up before we check out the other two twenty two plus seven plus eight
plus twenty four that's gonna give us sixty one and then if we remind ourselves of how many predictions
we've made there's sixty one.
Why.
Because there's sixty one examples in the tests.
Okay so what are these two here.
Right.
So this is let's read it out so we got predicted label where the model is predicted 1.
But the actual label was 0.
Okay so that's a false positive.
So there's seven of those and now we have a predicted label of zero.
But the actual label is 1.
So that's eight examples of that.
That's eight false negatives and so hence this is where the confusion matrix or the word confusion comes
into confusion Matrix these examples here this diagonal is where our model is getting confused a.k.a.
predicting zero where the actual labels won or predicting one where the actual label zero and a confusion
matrix.
We look here where the model got the samples right is on the diagonal.
So this here.
So if we come to our confusion matrix anatomy now we're going to replicate this in the next video.
This our confusion matrix anatomy we see correct here is a diagonal.
So this is true positives true negatives of false negatives is over here.
So on the right side of the confusion matrix and there are false positives is down here.
Why is it a false negative.
Well the predicted label is zero.
Right.
And the model has the true label is actually 1 and this is a false positive because the model is predicted
1 and the true label is 0.
And we've seen this before what a true positive a false positive a true negative and a false negative
is.
So how would we replicate something like this which is again a little bit more visual than our cross
tab.
One way to do so is with let's make a comment here make our confusion matrix more visual with C Bond's
heat map.
Now not gone.
DANIEL What the hell is Seabourn.
Well if we go here seaborne heat map.
This is what I do whenever I don't know something.
I go seaborne heat map.
We look at seaborne you might be asking What even is seaborne seaborne heat map plot rectangular data
as a color encoded matrix right.
Well I'll give you the nuts and bolts Seabourn is a visualization library that is built on top of matte
plot lib and it's pretty relatively easy to use but we're going to mostly just take care of the heat
map function so let's see it.
So import seaborne as SARS.
And then we're going to go set the font scale of seaborne because it can be a bit smaller set the font
scale that we want set.
We want font scale.
This is one of the easiest ways to visualize a confusion matrix other than a PD cross tab.
So we're going to go create a confusion matrix.
We're going to confirm that.
Short for confusion matrix as we like to minimize how many keystrokes that we take.
Why test why spreads passed the test and predictions just like we've done up here and then we're going
to plot it using seaborne S.A. don't hate Matt confirm that
a what module not found era drat we can't use Seabourn now I'm kidding.
So the reason why this module is not found is because you remember right back in the start when we create
an environment using conduct.
Well when we did that we didn't install Seabourn and because Seabourn is built on top of matte plot
Lib we do have map plot layer but we don't have seaborne we have pan is we have cyclone we have Jupiter
but we're missing Seabourn.
So how would you go about installing a module into your condo environment.
What we've got running in our terminal.
Right so this is our condo environment running in our terminal.
This is what serving up our Jupiter notebook server.
How do you think you would install it.
So one way would be to go to terminal start a new window with Mike OCD desktop email cause where my
environment is stored sample project or zoom in here so you can see a sample project.
So we've got our environment here.
And then if we go conduct and list wonderful and then if we wanted to activate it we could go conduct
activate we want our sample project or copy and paste that now and I could go conduct install seaborne
that's one option a one hit enter because I'm going to show you the other option and I'll show you how
you can do it right with energy but a notebook this a little bit bonus tip right.
When doing this during a classification model evaluation video but because we want to use seaborne let's
see how we can do it from within a group notebook.
So this is something you might try whenever you get this module not found error say you came across
some code or you come across a function that you want to use from a library you don't have install this
is how you can do it from with energy but a notebook to install it into the environment so it's ready
to go.
So after running this cell here we're gonna be able to use this cell if everything works to plan yes
prefix.
So what this is saying is we're importing says this is going to let us access our system a.k.a. our
computer and then we're running this bang function which is just a way of telling Jupiter to run a bash
command so as if we did the same command here.
Now if we run bang LSI here it's gonna show us the exact same output but just from with energy but a
notebook we get rid of that so we don't need it.
And we go condo install we're gonna pass code to install yes and prefix we've seen prefix when we're
setting up our environment going to go CIS dot prefix prefix is just another word for path name a.k.a.
this thing here path name and then we're gonna go save on so fingers crossed.
We run their cell.
We should install Seabourn and then we should be able to plot our confusion matrix using sea Bourne's
heat map function.
So let's shift and into
Hall again some loading.
Beautiful.
This is telling us what it's going to do.
A newer version of countries is thank you.
Package plan added slash updated specs seaborne.
Yep.
The following new packages will be installed yet.
Patty seaborne stats models preparing transaction.
Dun dun dun.
Tick tick tick.
Moment truth.
Might take a little while to import.
Oh there we go.
But it's not really offering much.
Why.
We go back to our keynote.
To see our confusion metrics anatomy.
We wanted to look a bit a bit more like this right to get some numbers on there.
Now why isn't this offering them.
Well let's pause a video there.
We'll continue on fixing up our confusion matrix in the next video see how we get some numbers on there.
But take a little note of this one here.
It'll be in the example code reference that you can use.
But this is how you can install how to install a Conda package.
From a Jupiter notebook into the current environment
and then you just put the package over here.
All right let's take a quick break.
There is a lot to cover in that video.
I'll see you in the next one.