# Naive Bayes Classifier

## Multinomial Naive Bayes

Let’s take the case of spam classification → we have emails with the combination of words tha we want to use to classify future emails as spam or not. Let’s say they have the combination of the four words : **Dear, Friend, Lunch, Money**. Now, we just count the frequency of these 4 four words in the normal emails and then assign **Likelihoods** to each as follows. Say D = 8, F = 5, L =3, M=1:

Now , we do the same for 7 spam emails: \(\begin{aligned} &P(D|S) = 2/6 = 0.29 \\ &P(F|S) = 1/7 = 0.17 \\ &P(L|S) = 0/7 = 0 \\ &P(M|S) = 4/7 = 0.57 \\ \end{aligned}\)

Then, we define the ratios for the Normal (N) and Spam (S) :

\[\begin{aligned} &P(N) = 0.67 \\ &P(S) = 0.33 \\ \end{aligned}\]Thus, now every word combination we get, we just multiply the priors with the likelihoods and compare. For example, If we get **Dear Friend :**

The key realization is that the product of the priors and likelihood, according to the Bayes theorem, should be proportional to the Likelihood of email being normal given the letters seen i.e P(N) and the same for spam. Thus, directly comparing the two values above tells us that the email has more chance of being normal → We classify it as normal. But, what if the a word not previously encountered in spam is seen in the email? → Take the example of **Lunch Money Money Money Money :**

This is a problem since it limits our ability to classify → We alleviate this by introducing **placeholder observations** into the spam group. These observations are \(\alpha\) in number and can be included in the counting process for frequentist likelihoods to eradicate this problem → so, for the value of 1 additional observation, we get

Using hte same prior values, we get :

\[\begin{aligned} &P(N) * P(L|N) * P(M|N) ^4 = 1e-5 \\ &P(S) * P(L|S) * P(M|S) ^ 4 = 1.22e-5 \end{aligned}\]Now, we see that the email is more likely to be spam!

### why Naive ?

Naive bayes treats language as just bag of words and so the score for **Dear Friend** would be the same as **Friend Dear →** In the general sense, for any such problem, the Naiive bayes does not exploit inter-dependencies in value, as seen from the probability segregation into disjoint sets while calculation

## Gaussian Variant

We do the sam process, but this time we create gaussian distributions for the variables to represent likelihoods and thus, we take the points on these gaussian curves as the likelihood values that need to be multiplied by the prior to generate the score

- To manage underflow, we take the log of all probabilities and add them to calculate the score
- The score with the higher log value is the class into which the new observation should be classified

One way to study the impact of different variables is to weight the different classes → weighted log-loss. Cross validation helps in determining which class has more impact