All right.
So upwards and onwards in this lesson we're going to make our predictions based on comparing the two
join probabilities that we calculated in the last lesson.
This really is the meat of the naive bayes classifier as part of the NI phase classifier algorithm.
We will be comparing the two probabilities and classifying email based on which probability is higher.
Let's add a section heading here to commemorate this.
Making predictions and for the subheading All right checking for the higher joined probability.
Now I'm also gonna add some latex marked down as a note to ourselves and maybe our future selves when
we come back and look at this again to remind us as to what the code that we're going to write is gonna
do so I'll write to taller signs and then I'll put P parentheses spam and then for some spacing allowed
the backslash and the comma.
Then the pipe symbol then another backslash and a comma then an X backslash comma greater than backslash
comma P parentheses ham backslash comma pipe backslash comma X closing parentheses and two dollar signs.
Maybe even capitalized the S here and when I capitalized the X here.
All right so that makes it pretty explicit.
We're going to be comparing whether the probability that an email is spam given its tokens is greater
than than the probability that it is non spam given the tokens.
The converse of course is also true.
So when a copy this line pasted in.
Change this to less than and maybe put an oar in between star or star star would be quite nice though
if this was centered instead of being aligned to the left.
So I'll add some H2 mail mark down here.
Angle brackets center closing angle brackets and then on the other side it'll have opening angle brackets
forwards large center closing angle brackets and I'll add a little line break here as well with the
letters B are enclosed by angled brackets.
There we go.
So these two lines of latex notation summarize what we're gonna do.
And because we've done so much legwork making these predictions it's actually not that hard at this
stage.
And I think it actually makes for a nice mini challenge so I'd like you to give us a try.
Can you create the vector of predictions are y hat.
Now remember that the spam emails in this vector should have the value 1 or true and the non spam the
ham emails should have the value 0 or false I'd like you to store your results in a variable called
prediction I'll give you a few seconds to pause the video and have a go
ready.
Here's the solution.
We're gonna be making use of this variable here which holds on to the joint probability that an email
is non spam or ham given its tokens and we're gonna be making use of this variable here which holds
onto the joint probability that an email is spam given its tokens and the way we're going to compare
these two is simply by writing prediction as equal to joint on a school log on the school spam greater
than joint I just got log on a score.
Ham and that's it.
Let's peek inside our prediction vector.
Let's look at the last five results.
So prediction square brackets minus five colon.
Those are equal to False false false false and false.
In other words the last five emails in our prediction vector are all non spam emails.
Let's compare this to the last five emails in y underscore test.
Now here are the actual labels.
These are the actual classifications and what we see is five zeros meaning the last five emails are
actually all non spam emails and these five predictions that we looked at were in fact correct.
Now if you don't fancy looking at integers here and billions here all you have to do to convert a boolean
to an integer is to multiply by 1.
Now I want to show you something else.
It turns out that we can actually simplify the calculation a little bit.
Looking at our equation for making a comparison between this quantity here and this quantity here looking
at our formulas above we can see that the probability of him given X is expressed by this fraction where
we're dividing by p of x in the denominator and the probability of spam given X is given by this fraction
where we are also dividing by p of x in the denominator since we're doing this on both sides of the
equation effectively we're doing this for both quantities.
We can actually remove this bit and still get the same results.
In the case of our code this p of x this bottom part where we're dividing by the probability of a token
occurring is given by this minus n p dot log probability of all tokens.
We took the log of the probabilities remember so instead of dividing we're actually subtracting so I
can take this line here and copy it.
A little mark down so call it simplify pasted in and I'll grab this line here and also paste it in and
then what I'll do is I'll delete the same section of code on both lines.
The part or subtracting the probability of a token occurring and the reason I can do this is because
we're after being able to predict the why we were interested in being able to predict whether we have
a spam email or a non spam email.
This prediction actually does not depend on the probability of a token occurring.
This is why I can do this simplification.
Now let me add some late tax code to make this a little bit more clear.
And this is because you know the thing is mathematically those two lines right.
The one or subtracting the log probability of the tokens and the ones where we're not doing that actually
not the same.
You won't actually have the same numbers in joint underscore log underscore spam and enjoined on a scroll
log on a square ham.
However their relationship to each other is unchanged even though these two quantities are not equal
to each other mathematically.
The simplification is still valid because it doesn't change our predictions.
The reason why adding the simplification step here is because I've seen this step in many many of many
implementations of the need based classifier.
And I remember when first looking at this code it was very confusing because I couldn't type back to
the formula in Bayes theorem.
Nonetheless this simplification is perfectly valid and will not change our results.
You might even call this the one weird trick that statisticians don't want you to know.
And you can verify this for yourself and the next lessons where we're gonna be talking about metrics
and evaluation.
We're going to look at our Bayes classifier and actually check how well it's doing.
I'll see you there.