Okay, my friends,
we have three steps left and one we're about to implement
is the confusion matrix, which is a simple 2D matrix.
Know two rows, two columns, which will show us the number
of correct predictions we did in both case, you know, predicting 0 or 1,
and how many incorrect predictions we did in both cases, zero and one.
All right.
So that will be a nice way to see quickly where we did right and wrong.
And also in that same code cell we will compute the accuracy.
And now well at the end of the previous tutorial I actually asked you to
try to figure it out on your own by looking at the scikit learn API.
So that's exactly what we're going to do.
First here
I'm going to show you how to navigate it and find the information we want.
You know the tool that we need.
So let's go back to the welcome page scikit learn.
Then remember you have to go to API here which contains
all the classes and functions from the different modules.
And I actually give you a hint.
In the previous tutorial
I told you to go to look into a module called metrics remember?
So we just have to scroll down a bit here
and we will find very soon metrics.
It's in the alphabetical order.
So there it is metrics.
And then it's very well organized.
As you can see you have the regression metrics in which we already covered
the most important ones and the classification metrics.
And here we are of course dealing with classification.
Therefore you had to look into here.
And now we're getting closer. We're getting warmer.
What do we see inside.
Well we actually see metrics confusion matrix.
And that's exactly what we'll take in order to build this confusion matrix.
All right.
And then what I usually do is simply have a look at one of the examples,
because it usually contains the code on how to build such a tool.
You know the confusion matrix, but it usually give you this example
on some random values of dependent variable vectors.
You know, that's like why test?
You know, the vector containing the real results.
That's why pred containing the vector of predictions.
And it tells you how to apply this confusion matrix function
onto the vector of real results and the vector of predictions.
Okay.
So in order to implement this, well let's just take this so that we can
import indeed that confusion matrix
which belongs to the metrics module from the second library.
So there we go.
Let's face that. First I just copied it.
Let's create a new code cell and paste this.
So that's how you import the confusion matrix.
And then let's grab that other piece of code.
You know this particular line where indeed we apply the confusion matrix function
onto the vectors of predictions and real results.
So let's paste this.
So you see what I'm trying to do here.
I don't do this because I'm lazy.
I do this in order to train you to be independent.
You know, whenever you need a new information or a new tool
that you need, I'm training you on how to find it in the scikit
learn API, and I will do the same later on when we start working with TensorFlow.
You know, in the deep learning part of this course, part eight.
But so you see, this is very important.
I really want you to be independent and figure things out on your own.
And now inside this confusion matrix function, what do we have to replace.
Well, you know, they call the vector here of real result y.
True.
But us, since we actually want to distinguish
the vector of real results in the training set and the test set,
well we actually called are y true vectors.
Let's say y train for the training set and y test for the test set.
So here, since of course the confusion matrix is usually evaluated
on the test set.
You know for new observations here we have to replace y true by white test
so that we will get indeed the confusion matrix showing the correct predictions
and the incorrect predictions for both cases zero and one on the test set.
Great.
So we will actually put the output of this confusion matrix function apply
to white this and white bread into a new variable, which we're going to call c
m which stands for confusion matrix and which will be exactly
the output returned by this confusion matrix function.
So there we go.
And then we'll add a final print
c m in order to print indeed that confusion matrix okay.
So these are the three lines of code that allow indeed to build that confusion
matrix and print it.
And now remember that I also asked you to compute the accuracy and will to do this.
We just had to do exactly the same as which we just did.
You know, to find that information, actually try to press pause on the video
and find it yourself if not already.
So we're going to go back to that matrix module.
You know, remember the metrics module from the scikit learn library.
And we're going to look back into this classification
matrix section to find the accuracy.
So according to you where is it.
Well it's hard to miss it.
This is actually the first one accuracy score which computes indeed
the accuracy classification score, which is just,
you know, the rate of correct predictions.
So let's do this.
Let's click this link and we will get indeed
all the documentation on this accuracy score function,
which returns the accuracy of your model on whatever set of data.
And we'll apply it of course, to the test set.
So first of all here as we can see
this accuracy score function belongs of course to the same metrics module.
So we don't have to take all of this. Again.
We can just take that name of the function here.
And I will show you what to do here.
Just next to the confusion matrix you add a comma
and then you can paste this other function you need.
You know what
you have to import still from that metrics module from the scikit learn library.