Introduction
Naive Bayes classifiers are highly scalable, requiring a number of parameters linear in the number of features (or variables) in a learning problem. Consequently, parameter fitting in Naive Bayes can be accomplished using a simple counting operation, which means they can work very well in high-dimensional problems.
The algorithm is probabilistic in nature. It calculates the probability of a class label given the input features. This makes it particularly interpretable, as it provides a confidence score rather than just a hard prediction.
The term "naive" comes from the strong assumption that all features are independent of each other given the class. In reality, features are rarely perfectly independent, but the classifier often still performs robustly.
Mathematical Foundation
Naive Bayes relies directly on Bayes' Theorem. For a class label \(y\) and a feature vector \(\mathbf{x} = [x_1, x_2, \dots, x_n]\), the theorem states:
Where:
- \(P(y | \mathbf{x})\) is the posterior probability of class \(y\) given features \(\mathbf{x}\).
- \(P(\mathbf{x} | y)\) is the likelihood probability of features given class \(y\).
- \(P(y)\) is the prior probability of class \(y\).
- \(P(\mathbf{x})\) is the evidence or marginal likelihood.
The Naive Assumption
The "naive" part simplifies the likelihood \(P(\mathbf{x} | y)\). Assuming conditional independence, we can expand this as:
Substituting this back, the posterior becomes proportional to:
Since \(P(\mathbf{x})\) is constant for all classes, we can ignore it during classification and simply choose the class \(y\) that maximizes the numerator. This is known as the Maximum A Posteriori (MAP) decision rule:
Common Variants
While the core logic remains the same, different distributions are used to model the likelihood \(P(x_i | y)\) depending on the data type:
- Gaussian Naive Bayes: Assumes features follow a normal distribution. Used for continuous data.$$P(x_i | y) = \frac{1}{\sqrt{2\pi\sigma^2_y}} \exp\left( -\frac{(x_i - \mu_y)^2}{2\sigma^2_y} \right)$$
- Multinomial Naive Bayes: Used for discrete counts, such as word counts in text classification.
- Bernoulli Naive Bayes: Designed for binary/boolean features, common in bag-of-words text classification where features indicate presence or absence.
Implementation Example
Below is a Python implementation using scikit-learn to classify emails as spam or ham:
Applications
Naive Bayes is ubiquitous in real-world systems due to its speed and efficiency:
- Spam Filtering: The classic application, analyzing word frequencies to flag unwanted emails.
- Sentiment Analysis: Classifying reviews or tweets as positive, negative, or neutral.
- Medical Diagnosis: Predicting disease likelihood based on symptoms and patient history.
- Real-time Prediction: Due to low computational cost, it is suitable for streaming data environments.
Strengths and Limitations
- Strengths: Fast training and prediction; handles high-dimensional data well; requires less training data than methods like Maximum Entropy; robust to irrelevant features.
- Weaknesses: The "naive" independence assumption is often violated in practice; can produce poor probability estimates if training data is biased; zero-frequency problem (if a category never appears in training, its probability is zero) requires smoothing techniques like Laplace Smoothing.
References & Further Reading
- 1 Domingos, P., & Pazzani, M. (1997). On the Optimality of the Simple Bayesian Classifier under Zero-One Loss. Machine Learning, 29, 103-130.
- 2 Manning, C. D., Raghavan, P., & Schütze, H. (2008). Introduction to Information Retrieval. Cambridge University Press.
- 3 Scikit-Learn Documentation: Naive Bayes. scikit-learn.org