We're almost there.
In this lesson we'll calculate the probability that a token occurs given that the email is spam.
So in this case we might calculate the probability of Viagra occurring in an email.
Given that the email is spam and then afterwards we'll calculate the probability of a particular token
occurring given that the email is a non spam message so that this as a markdown Cell C P parentheses
token pipe spam parentheses problem ability that a token occurs given the email is spam.
Now we're not going to just do this for a single token.
We did do this for all the tokens simultaneously.
I'm going to store our results of this calculation in a variable called prob on a score tokens on a
school spam and I'll set that equal to the following.
We're going to take our summed spam tokens and divide those by the spam word count.
Right.
This will divide our summed spam tokens are two thousand five hundred values of how often each word
occurred in spam emails and we're dividing it by the total number of spam tokens.
But there is one modification that I have to make this calculation and that modification has to do with
what we did over here.
It has to do with our Laplace smoothing technique.
We added 1 2 hour summation because we don't want to end up dividing zero.
Since there were two thousand five hundred words where we added 1 we have to balance that out again
and this is why we're going to add the vocabulary size to our denominator here.
We've added 1 two thousand five hundred times so therefore we also have to add two thousand five hundred
to our spam would count to fully implement this smoothing technique.
And that's it.
We've done a lot of legwork so it's just one line of code for that calculation which is really really
neat.
The thing you're probably wondering about though is what are the probabilities actually look like.
Right.
Prob on a school tokens on a school spam square brackets.
Colon 5 will show us the first couple of entries in this series so here we see the actual values that
we get now.
The numbers that we're working with aren't particularly large but if we sum them all up they should
add up to 1.
That's how probability works right.
Probably underscore tokens on the score spam dot some parentheses will show us if our math ties out
and I think it does.
We've calculated the probability of the tokens given that the email is spam for all our two thousand
five hundred entries.
Now let's do the same thing the other way round.
So I'll quickly copy my mark down sell here.
Pasted in.
Change this to mark down and change this to him and change this to non spam.
This calculation is very similar.
Right.
Prob on a score tokens on a school non spam will hold on to the token probabilities.
And that's gonna be sometime tokens divided by Open parentheses the non spam word count so the total
number of words in the non spam messages and we're to add 2500 since we're also using the Laplace smoothing
technique.
Here There we go.
That line calculates all the probabilities and can do a quick check here that these probabilities indeed
some to 1 are very close to 1 but calling the summation method and hitting shift enter.
There we go.
I'm pretty happy with that.
OK so where does this leave us.
We've tackled the fraction in the numerator but we haven't tackled the fraction in the denominator yet.
This here is the overall probability of a particular token.
I'll add that as a markdown cell I'll call it p token.
This is the probability that a token occurs regardless of whether we're dealing with spam or non spam
emails.
So what I'll do is I'll create a variable called prob underscore tokens on a score all and that will
be equal to our full train features summed up across the columns axis is equal to zero divided by the
total word count.
That's it.
Again this probability should sum to 1.
So let's just quickly check if we've done this calculation right.
Looks good.
You'll notice that I haven't done any lap plus smoothing him because I'm pretty much guaranteed I'm
not dividing a zero because we've taken the two thousand five hundred most frequent words in our whole
dataset to be our features.
I think it's a good time to save our train model to a text file and kind of create a checkpoint for
ourselves.
At the top of her notebook where we've got her Constance I'm going to copy these two lines paste the
minigun and and there I'm going to create two new constants.
The first one will be called token and a school spam on a scroll prob file.
I'm going to save this work to a different folder namely our testing folder.
So I'm going to replace 0 2 on it's got training with 0 3 on a score testing and this text file shall
be called prob hyphen spam T X T.
I'll do the same thing for non spam files.
So token I'll just go ham on a scope problem on the score file will save us prob hyphen non spam dot
t t.
The goal is to save both of these text files 100 0 3 and a score testing inside our spam data folder.
Going back up to the constants I'll quickly add another constant up here for all our tokens.
So token on underscore all prob file I want to call this file prob hyphen all hyphen tokens knowledge
shift enter on this.
Then I'll come down here and this is where we save that trained model where again gonna use num PIs
save text function and we're gonna call it three times first time for the spam probabilities where we're
gonna save prob on the score tokens on a school spam we're gonna call it again for our ham tokens.
So token I'd just go ham and just go prob just go file comma prob tokens non spam and we're gonna do
it one more time for all the tokens.
So this is gonna be ham and spam combined.
There we go.
Saving data to the disk as a file is a really nice way of creating like a checkpoint for yourself.
At least this way you can pick up where you left off and you don't have to rerun the calculations that
you've done before and you can always work off like the same text file.
You can always work off the same data.
It's not gonna change.