5
$\begingroup$

I am writing a naive bayes classifier for a text classification problem. I have a bunch of words and an associated label:

[short,snippet,text], label1
[slightly,different,snippet,text], label2
...

I am able to train the naive bayes fine. However, when I am classifying unseen data, sometimes there are unseen features (words). In that case, what happens to the naive bayes formula to determine the probability of a class $C$ given features $F_1,F_2,...$?

$$P(C|F_1,F_2,...) = \frac{P(F_1,F_2,...|C)P(C)}{P(F_1,F_2,...)} = \frac{P(C)\prod_{i}P(F_i|C)}{P(F_1,F_2,...)}$$

Say feature $F_k$ never occured in the training data, then isn't $P(F_k|C)=\frac{0}{0}$?

How is this typically handled in classification problems?

One option is to simply ignore unseen features. However, I would not like to do that, since I am trying to calculate the actual probability score associated with classes. Probabilities should take a hit when there are unseen features, but I am not sure how to do that mathematically.

Any insights, links to reseach articles, etc would really help! Thanks in advance.

$\endgroup$

1 Answer 1

8
$\begingroup$

Typically one would use Laplace smoothing, essentially adding an artificial observation of every feature for every class. This is done to avoid the issue of having never observed a feature in one class causing a zero that propagates. This is also called a uniform prior.

For a feature never seen ever in any training data, the "uniform prior" means everything will have the same probability (hence uniform without data), and so it will have no impact on which class you select.

In terms of making the decision for your classifier, this would have the equivalent result of just throwing away the novel feature! So that is what you should do. Technically it would change the probability slightly to keep it, but Naive Bayes doesn't give good probabilities in the fist place, so its not worth worrying about.

However, I would not like to do that, since I am trying to calculate the actual probability score associated with classes. Probabilities should take a hit when there are unseen features, but I am not sure how to do that mathematically.

This is a good intuition and correct. But in general, we can't do much when we encounter unobserved features as we intrinsically have no knowledge about them! All you can really do is pick a prior belief and run with that when you don't have data.

If you truly want good probabilities, start looking at logistic regression. Its not perfect either, but the probabilities are much more reasonable than what Naive Bayes will give you.

$\endgroup$
2
  • 1
    $\begingroup$ Raff, thanks for the highly insightful answer. Is there any particular reason the Naive Bayes is not good for probabilities? I.e. the independence assumption? $\endgroup$
    – applecider
    Commented Jul 20, 2014 at 20:21
  • 2
    $\begingroup$ That is exactly why. For high dimensional problems (such as text) Naive Bayes tends to return probabilities either very nearly 100% or nearly 0%. $\endgroup$ Commented Jul 20, 2014 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.