Naive Bayes classifier


In statistics, Naïve Bayes classifiers are a family of simple "probabilistic classifiers" based on applying Bayes' theorem with strong independence assumptions between the features. They are among the simplest Bayesian network models. But they could be coupled with Kernel density estimation and achieve higher accuracy levels.
Naïve Bayes classifiers are highly scalable, requiring a number of parameters linear in the number of variables in a learning problem. Maximum-likelihood training can be done by evaluating a closed-form expression, which takes linear time, rather than by expensive iterative approximation as used for many other types of classifiers.
In the statistics and computer science literature, naive Bayes models are known under a variety of names, including simple Bayes and independence Bayes. All these names reference the use of Bayes' theorem in the classifier's decision rule, but naïve Bayes is not a Bayesian method.

Introduction

Naive Bayes is a simple technique for constructing classifiers: models that assign class labels to problem instances, represented as vectors of feature values, where the class labels are drawn from some finite set. There is not a single algorithm for training such classifiers, but a family of algorithms based on a common principle: all naive Bayes classifiers assume that the value of a particular feature is independent of the value of any other feature, given the class variable. For example, a fruit may be considered to be an apple if it is red, round, and about 10 cm in diameter. A naive Bayes classifier considers each of these features to contribute independently to the probability that this fruit is an apple, regardless of any possible correlations between the color, roundness, and diameter features.
For some types of probability models, naive Bayes classifiers can be trained very efficiently in a supervised learning setting. In many practical applications, parameter estimation for naive Bayes models uses the method of maximum likelihood; in other words, one can work with the naive Bayes model without accepting Bayesian probability or using any Bayesian methods.
Despite their naive design and apparently oversimplified assumptions, naive Bayes classifiers have worked quite well in many complex real-world situations. In 2004, an analysis of the Bayesian classification problem showed that there are sound theoretical reasons for the apparently implausible efficacy of naive Bayes classifiers. Still, a comprehensive comparison with other classification algorithms in 2006 showed that Bayes classification is outperformed by other approaches, such as boosted trees or random forests.
An advantage of naive Bayes is that it only requires a small number of training data to estimate the parameters necessary for classification.

Probabilistic model

Abstractly, naïve Bayes is a conditional probability model: given a problem instance to be classified, represented by a vector representing some features, it assigns to this instance probabilities
for each of possible outcomes or classes.
The problem with the above formulation is that if the number of features is large or if a feature can take on a large number of values, then basing such a model on probability tables is infeasible. We therefore reformulate the model to make it more tractable. Using Bayes' theorem, the conditional probability can be decomposed as
In plain English, using Bayesian probability terminology, the above equation can be written as
In practice, there is interest only in the numerator of that fraction, because the denominator does not depend on and the values of the features are given, so that the denominator is effectively constant.
The numerator is equivalent to the joint probability model
which can be rewritten as follows, using the chain rule for repeated applications of the definition of conditional probability:
Now the "naïve" conditional independence assumptions come into play: assume that all features in are mutually independent, conditional on the category. Under this assumption,
Thus, the joint model can be expressed as
where denotes proportionality.
This means that under the above independence assumptions, the conditional distribution over the class variable is:
where the evidence is a scaling factor dependent only on, that is, a constant if the values of the feature variables are known.

Constructing a classifier from the probability model

The discussion so far has derived the independent feature model, that is, the naïve Bayes probability model. The naïve Bayes classifier combines this model with a decision rule. One common rule is to pick the hypothesis that is most probable; this is known as the maximum a posteriori or MAP decision rule. The corresponding classifier, a Bayes classifier, is the function that assigns a class label for some as follows:

Parameter estimation and event models

A class's prior may be calculated by assuming equiprobable classes, or by calculating an estimate for the class probability from the training set. To estimate the parameters for a feature's distribution, one must assume a distribution or generate nonparametric models for the features from the training set.
The assumptions on distributions of features are called the "event model" of the naïve Bayes classifier. For discrete features like the ones encountered in document classification, multinomial and Bernoulli distributions are popular. These assumptions lead to two distinct models, which are often confused.

Gaussian naïve Bayes

When dealing with continuous data, a typical assumption is that the continuous values associated with each class are distributed according to a normal distribution. For example, suppose the training data contains a continuous attribute, . We first segment the data by the class, and then compute the mean and variance of in each class. Let be the mean of the values in associated with class Ck, and let be the Bessel corrected variance of the values in associated with class Ck. Suppose we have collected some observation value. Then, the probability distribution of given a class,, can be computed by plugging into the equation for a normal distribution parameterized by and. That is,
Another common technique for handling continuous values is to use binning to discretize the feature values, to obtain a new set of Bernoulli-distributed features; some literature in fact suggests that this is necessary to apply naive Bayes, but it is not, and the discretization may throw away discriminative information.
Sometimes the distribution of class-conditional marginal densities is far from normal. In these cases, kernel density estimation can be used for a more realistic estimate of the marginal densities of each class. This method, which was introduced by John and Langley, can boost the accuracy of the classifier considerably.

Multinomial naïve Bayes

With a multinomial event model, samples represent the frequencies with which certain events have been generated by a multinomial where is the probability that event occurs. A feature vector is then a histogram, with counting the number of times event was observed in a particular instance. This is the event model typically used for document classification, with events representing the occurrence of a word in a single document. The likelihood of observing a histogram is given by
The multinomial naïve Bayes classifier becomes a linear classifier when expressed in log-space:
where and.
If a given class and feature value never occur together in the training data, then the frequency-based probability estimate will be zero, because the probability estimate is directly proportional to the number of occurrences of a feature's value. This is problematic because it will wipe out all information in the other probabilities when they are multiplied. Therefore, it is often desirable to incorporate a small-sample correction, called pseudocount, in all probability estimates such that no probability is ever set to be exactly zero. This way of regularizing naive Bayes is called Laplace smoothing when the pseudocount is one, and Lidstone smoothing in the general case.
Rennie et al. discuss problems with the multinomial assumption in the context of document classification and possible ways to alleviate those problems, including the use of tf–idf weights instead of raw term frequencies and document length normalization, to produce a naive Bayes classifier that is competitive with support vector machines.

Bernoulli naïve Bayes

In the multivariate Bernoulli event model, features are independent Booleans describing inputs. Like the multinomial model, this model is popular for document classification tasks, where binary term occurrence features are used rather than term frequencies. If is a boolean expressing the occurrence or absence of the 'th term from the vocabulary, then the likelihood of a document given a class is given by
where is the probability of class generating the term. This event model is especially popular for classifying short texts. It has the benefit of explicitly modelling the absence of terms. Note that a naive Bayes classifier with a Bernoulli event model is not the same as a multinomial NB classifier with frequency counts truncated to one.

Semi-supervised parameter estimation

Given a way to train a naïve Bayes classifier from labeled data, it's possible to construct a semi-supervised training algorithm that can learn from a combination of labeled and unlabeled data by running the supervised learning algorithm in a loop:
Convergence is determined based on improvement to the model likelihood, where denotes the parameters of the naïve Bayes model.
This training algorithm is an instance of the more general expectation–maximization algorithm : the prediction step inside the loop is the E-step of EM, while the re-training of naïve Bayes is the M-step. The algorithm is formally justified by the assumption that the data are generated by a mixture model, and the components of this mixture model are exactly the classes of the classification problem.

Discussion

Despite the fact that the far-reaching independence assumptions are often inaccurate, the naive Bayes classifier has several properties that make it surprisingly useful in practice. In particular, the decoupling of the class conditional feature distributions means that each distribution can be independently estimated as a one-dimensional distribution. This helps alleviate problems stemming from the curse of dimensionality, such as the need for data sets that scale exponentially with the number of features. While naive Bayes often fails to produce a good estimate for the correct class probabilities, this may not be a requirement for many applications. For example, the naive Bayes classifier will make the correct MAP decision rule classification so long as the correct class is more probable than any other class. This is true regardless of whether the probability estimate is slightly, or even grossly inaccurate. In this manner, the overall classifier can be robust enough to ignore serious deficiencies in its underlying naive probability model. Other reasons for the observed success of the naive Bayes classifier are discussed in the literature cited below.

Relation to logistic regression

In the case of discrete inputs, naive Bayes classifiers form a generative-discriminative pair with logistic regression classifiers: each naive Bayes classifier can be considered a way of fitting a probability model that optimizes the joint likelihood, while logistic regression fits the same probability model to optimize the conditional.
The link between the two can be seen by observing that the decision function for naive Bayes can be rewritten as "predict class if the odds of exceed those of ". Expressing this in log-space gives:
The left-hand side of this equation is the log-odds, or logit, the quantity predicted by the linear model that underlies logistic regression. Since naive Bayes is also a linear model for the two "discrete" event models, it can be reparametrised as a linear function. Obtaining the probabilities is then a matter of applying the logistic function to, or in the multiclass case, the softmax function.
Discriminative classifiers have lower asymptotic error than generative ones; however, research by Ng and Jordan has shown that in some practical cases naive Bayes can outperform logistic regression because it reaches its asymptotic error faster.

Examples

Person classification

Problem: classify whether a given person is a male or a female based on the measured features.
The features include height, weight, and foot size.

Training

Example training set below.
Personheight weight foot size
male618012
male5.92 19011
male5.58 17012
male5.92 16510
female51006
female5.5 1508
female5.42 1307
female5.75 1509

The classifier created from the training set using a Gaussian distribution assumption would be :
Personmean variance mean variance mean variance
male5.8553.5033 × 10−2176.251.2292 × 10211.259.1667 × 10−1
female5.41759.7225 × 10−2132.55.5833 × 1027.51.6667

Let's say we have equiprobable classes so P= P = 0.5. This prior probability distribution might be based on our knowledge of frequencies in the larger population, or on frequency in the training set.

Testing

Below is a sample to be classified as male or female.
Personheight weight foot size
sample61308

We wish to determine which posterior is greater, male or female. For the classification as male the posterior is given by
For the classification as female the posterior is given by
The evidence may be calculated:
However, given the sample, the evidence is a constant and thus scales both posteriors equally. It therefore does not affect classification and can be ignored. We now determine the probability distribution for the sex of the sample.
where and are the parameters of normal distribution which have been previously determined from the training set. Note that a value greater than 1 is OK here – it is a probability density rather than a probability, because height is a continuous variable.
Since posterior numerator is greater in the female case, we predict the sample is female.

Document classification

Here is a worked example of naive Bayesian classification to the document classification problem.
Consider the problem of classifying documents by their content, for example into spam and non-spam e-mails. Imagine that documents are drawn from a number of classes of documents which can be modeled as sets of words where the probability that the i-th word of a given document occurs in a document from class C can be written as
Then the probability that a given document D contains all of the words, given a class C, is
The question that we desire to answer is: "what is the probability that a given document D belongs to a given class C?" In other words, what is ?
Now by definition
and
Bayes' theorem manipulates these into a statement of probability in terms of likelihood.
Assume for the moment that there are only two mutually exclusive classes, S and ¬S, such that every element is in either one or the other;
and
Using the Bayesian result above, we can write:
Dividing one by the other gives:
Which can be re-factored as:
Thus, the probability ratio p / p can be expressed in terms of a series of likelihood ratios.
The actual probability p can be easily computed from log / p) based on the observation that p + p = 1.
Taking the logarithm of all these ratios, we have:
Finally, the document can be classified as follows. It is spam if , otherwise it is not spam.