Naive Bayes
Text Classification
Log Likelihood
Machine Learning
Natural Language Processing

Log likelihood to implement Naive Bayes for Text Classification

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Naive Bayes is a probabilistic classifier based on applying Bayes’ theorem with strong independence assumptions between the features. It is particularly popular for text classification due to its simplicity and effectiveness. One of the core components of the Naive Bayes classifier is the calculation of probabilities, for which the log likelihood is often used to improve computational efficiency and numerical stability. This article explores the concept of log likelihood in the context of implementing Naive Bayes for text classification.

Understanding Naive Bayes

Naive Bayes relies on the assumption that the presence (or absence) of a particular feature in a class is independent of the presence (or absence) of any other feature. For text classification, this means the model assumes that the presence of any word in a document is independent of the presence of any other word.

The key principle behind Naive Bayes is Bayes' theorem, which can be expressed as:

P(CX)=P(XC)P(C)P(X)P(C \mid X) = \frac{P(X \mid C) P(C)}{P(X)}

Where: • P(CX)P(C \mid X): Posterior probability of class CC given predictor XX. • P(XC)P(X \mid C): Likelihood of predictor XX given class CC. • P(C)P(C): Prior probability of class CC. • P(X)P(X): Prior probability of predictor XX.

Log Likelihood in Naive Bayes

Why Use Log Likelihood?

When implementing Naive Bayes, especially in text classification, the likelihood component P(XC)P(X \mid C) can become very small when dealing with long documents or large vocabularies. Since multiplying small probabilities leads to numerical underflow, we use the log likelihood to convert these products into sums, which are easier to compute and more stable.

Log Likelihood Formula

The log likelihood in Naive Bayes is calculated as the sum of the log probabilities of the individual features. For a document dd with words w1,w2,,wnw_1, w_2, \ldots, w_n, and class CC, the log likelihood can be computed as:

logP(dC)=log_i=1nP(w_iC)=_i=1nlogP(w_iC)\log P(d \mid C) = \log \prod\_{i=1}^{n} P(w\_i \mid C) = \sum\_{i=1}^{n} \log P(w\_i \mid C)

This conversion not only helps in preventing underflow but also allows vectorizing the computation, which is more efficient.

Using Log Likelihood in Classification

To classify a new text sample, you calculate the posterior probabilities for each class (in log form) and select the class with the highest probability:

logP(Cd)=logP(C)+logP(dC)\log P(C \mid d) = \log P(C) + \log P(d \mid C)

Where logP(dC)\log P(d \mid C) is the log likelihood we computed earlier.

Example of Implementing Naive Bayes with Log Likelihood

Consider a simplified vocabulary and two classes, Positive and Negative . Suppose we have the following computed probabilities (for demonstration):

WordP(wordPositive)P(\text{word} \mid \text{Positive})P(wordNegative)P(\text{word} \mid \text{Negative})
Good0.80.2
Bad0.10.6
Neutral0.10.2

Let's classify a new document: "Good Neutral".

  1. Calculate Log Likelihood for Each Class:
    • For Positive: logP("Good Neutral"Positive)=log(0.8)+log(0.1)\log P(\text{"Good Neutral"} \mid \text{Positive}) = \log(0.8) + \log(0.1) =logP("Good"Positive)+logP("Neutral"Positive)= \log P(\text{"Good"} \mid \text{Positive}) + \log P(\text{"Neutral"} \mid \text{Positive}) =0.22+(2.30)=2.52= -0.22 + (-2.30) = -2.52
    • For Negative: logP("Good Neutral"Negative)=log(0.2)+log(0.2)\log P(\text{"Good Neutral"} \mid \text{Negative}) = \log(0.2) + \log(0.2) =logP("Good"Negative)+logP("Neutral"Negative)= \log P(\text{"Good"} \mid \text{Negative}) + \log P(\text{"Neutral"} \mid \text{Negative}) =1.61+(1.61)=3.22= -1.61 + (-1.61) = -3.22
  2. Include Log Priors and Choose Class:
    Assume equal priors for simplicity: logP(Positive)=logP(Negative)=log(0.5)=0.69\log P(\text{Positive}) = \log P(\text{Negative}) = \log(0.5) = -0.69.
    logP(Positive"Good Neutral")=0.692.52=3.21\log P(\text{Positive} \mid \text{"Good Neutral"}) = -0.69 - 2.52 = -3.21logP(Negative"Good Neutral")=0.693.22=3.91\log P(\text{Negative} \mid \text{"Good Neutral"}) = -0.69 - 3.22 = -3.91

The document "Good Neutral" is classified as Positive because 3.21>3.91-3.21 > -3.91.

Summary Table

ComponentExpressionNote
Bayes' TheoremP(CX)=P(XC)P(C)P(X)P(C \mid X) = \frac{P(X \mid C) P(C)}{P(X)}Fundamental theorem for Naive Bayes.
Log LikelihoodlogP(dC)=i=1nlogP(wiC)\log P(d \mid C) = \sum_{i=1}^{n} \log P(w_i \mid C)Prevents numerical underflow by converting product to sum.
Classification RuleSelect class with highest logP(Cd)\log P(C \mid d)Stability and efficiency in computing class probabilities.

Additional Considerations

Laplace Smoothing

In practice, the probability P(wiC)P(w_i \mid C) can be zero if a word has not appeared in a training sample for a class. To handle this, Laplace smoothing is often applied to avoid zero probabilities by adding a small constant to each count before calculating probabilities.

Advantages of Using Log Likelihood

Numerical Stability: Converting probabilities to logs helps manage calculations with very small numbers. • Efficiency: Logarithms allow probabilities to be added rather than multiplied, which can be computationally cheaper. • Simplicity: Maintains the simplicity of Naive Bayes while enabling more robust performance.

Overall, log likelihood is an integral part of implementing Naive Bayes for text classification, ensuring robustness and efficiency while maintaining simplicity in the classification process.


Course illustration
Course illustration

All Rights Reserved.