# Naive Bayes classifier

(Redirected from Naive bayes)

In machine learning, naive Bayes classifiers are a family of simple probabilistic classifiers based on applying Bayes' theorem with strong (naive) independence assumptions between the features.

Naive Bayes models are also known under a variety of names in the literature, including simple Bayes and independence Bayes.[1] All these names reference the use of Bayes' theorem in the classifier's decision rule, but naive Bayes is not (necessarily) a Bayesian method;[1] Russell and Norvig note that "[naive Bayes] is sometimes called a Bayesian classifier, a somewhat careless usage that has prompted true Bayesians to call it the idiot Bayes model."[2]:482

Naive Bayes has been studied extensively since the 1950s. It was introduced under a different name into the text retrieval community in the early 1960s,[2]:488 and remains a popular (baseline) method for text categorization, the problem of judging documents as belonging to one category or the other (such as spam or legitimate, sports or politics, etc.) with word frequencies as the features. With appropriate preprocessing, it is competitive in this domain with more advanced methods including support vector machines.[3] It also finds application in automatic medical diagnosis.[4]

Naive Bayes classifiers are highly scalable, requiring a number of parameters linear in the number of variables (features/predictors) in a learning problem. Maximum-likelihood training can be done by evaluating a closed-form expression,[2]:718 which takes linear time, rather than by expensive iterative approximation as used for many other types of classifiers.

## Introduction

In simple terms, a naive Bayes classifier assumes that the value of a particular feature is unrelated to the presence or absence 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 3" 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 the presence or absence of the other 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.[5] 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.[6]

An advantage of naive Bayes is that it only requires a small amount of training data to estimate the parameters (means and variances of the variables) necessary for classification. Because independent variables are assumed, only the variances of the variables for each class need to be determined and not the entire covariance matrix.

## Probabilistic model

Abstractly, the probability model for a classifier is a conditional model

$p(C \vert F_1,\dots,F_n)\,$

over a dependent class variable $C$ with a small number of outcomes or classes, conditional on several feature variables $F_1$ through $F_n$. The problem is that if the number of features $n$ 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, this can be written

$p(C \vert F_1,\dots,F_n) = \frac{p(C) \ p(F_1,\dots,F_n\vert C)}{p(F_1,\dots,F_n)}. \,$

In plain English, using Bayesian Probability terminology, the above equation can be written as

$\mbox{posterior} = \frac{\mbox{prior} \times \mbox{likelihood}}{\mbox{evidence}}. \,$

In practice, there is interest only in the numerator of that fraction, because the denominator does not depend on $C$ and the values of the features $F_i$ are given, so that the denominator is effectively constant. The numerator is equivalent to the joint probability model

$p(C, F_1, \dots, F_n)\,$

which can be rewritten as follows, using the chain rule for repeated applications of the definition of conditional probability:

\begin{align} p(C, F_1, \dots, F_n) & = p(C) \ p(F_1,\dots,F_n\vert C) \\ & = p(C) \ p(F_1\vert C) \ p(F_2,\dots,F_n\vert C, F_1) \\ & = p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ p(F_3,\dots,F_n\vert C, F_1, F_2) \\ & = p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ \dots p(F_n\vert C, F_1, F_2, F_3,\dots,F_{n-1}) \end{align}

Now the "naive" conditional independence assumptions come into play: assume that each feature $F_i$ is conditionally independent of every other feature $F_j$ for $j\neq i$, given the category $C$. This means that

$p(F_i \vert C, F_j) = p(F_i \vert C)\,$,
$p(F_i \vert C, F_j,F_k) = p(F_i \vert C)\,$,
$p(F_i \vert C, F_j,F_k,F_l) = p(F_i \vert C)\,$,

and so on, for $i\ne j,k,l$. Thus, the joint model can be expressed as

\begin{align} p(C \vert F_1, \dots, F_n) & \varpropto p(C, F_1, \dots, F_n) \\ & \varpropto p(C) \ p(F_1\vert C) \ p(F_2\vert C) \ p(F_3\vert C) \ \cdots \\ & \varpropto p(C) \prod_{i=1}^n p(F_i \vert C)\,. \end{align}

This means that under the above independence assumptions, the conditional distribution over the class variable $C$ is:

$p(C \vert F_1,\dots,F_n) = \frac{1}{Z} p(C) \prod_{i=1}^n p(F_i \vert C)$

where the evidence $Z = p(F_1, \dots, F_n)$ is a scaling factor dependent only on $F_1,\dots,F_n$, 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 naive Bayes probability model. The naive 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 $\mathrm{classify}$ defined as follows:

$\mathrm{classify}(f_1,\dots,f_n) = \underset{c}{\operatorname{argmax}} \ p(C=c) \displaystyle\prod_{i=1}^n p(F_i=f_i\vert C=c).$

## Parameter estimation and event models

A class' prior may be calculated by assuming equiprobable classes (i.e., priors = 1 / (number of classes)), or by calculating an estimate for the class probability from the training set (i.e., (prior for a given class) = (number of samples in the class) / (total number of samples)). 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.[7]

The assumptions on distributions of features are called the event model of the Naive Bayes classifier. For discrete features like the ones encountered in document classification (include spam filtering), multinomial and Bernoulli distributions are popular. These assumptions lead to two distinct models, which are often confused.[8][9]

### Gaussian naive Bayes

When dealing with continuous data, a typical assumption is that the continuous values associated with each class are distributed according to a Gaussian distribution. For example, suppose the training data contain a continuous attribute, $x$. We first segment the data by the class, and then compute the mean and variance of $x$ in each class. Let $\mu_c$ be the mean of the values in $x$ associated with class c, and let $\sigma^2_c$ be the variance of the values in $x$ associated with class c. Then, the probability density of some value given a class, $P(x=v|c)$, can be computed by plugging $v$ into the equation for a Normal distribution parameterized by $\mu_c$ and $\sigma^2_c$. That is,

$p(x=v|c)=\frac{1}{\sqrt{2\pi\sigma^2_c}}\,e^{ -\frac{(v-\mu_c)^2}{2\sigma^2_c} }$

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.[1]

### Multinomial naive Bayes

With a multinomial event model, samples (feature vectors) represent the frequencies with which certain events have been generated by a multinomial $(p_1, \dots, p_n)$ where $p_i$ is the probability that event i occurs (or k such multinomials in the multiclass case). This is the event model typically used for document classification; the feature values are then term frequencies, generated by a multinomial that produces some number of words (see bag of words assumption). The likelihood of observing a feature vector (histogram) F is given by

$p(F|C) = \frac{(\sum_i F_i)!}{\prod_i F_i !} \prod_i {p_i}^{F_i}$

The multinomial naive Bayes classifier becomes a linear classifier when expressed in log-space:[3]

\begin{align} \log p(C|F) & \propto \log \left( p(C) \prod_{i=1}^n p(F_i \vert C) \right) \\ & = \log p(C) + \sum_{i=1}^n \log p(F_i \vert C) \\ & = b + \mathbf{w}_C^\mathsf{T} \mathbf{F} \end{align}

where $b = \log p(C)$ and $w_{Ci} = \log p(F_i \vert C)$.

If a given class and feature value never occur together in the training data, then the frequency-based probability estimate will be zero. 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.[3]

### Bernoulli naive Bayes

In the multivariate Bernoulli event model, features are independent booleans (binary variables) describing inputs. This model is also popular for document classification tasks,[8] where binary term occurrence features are used rather than term frequencies. If $F_i$ is a boolean expressing the occurrence or absence of the i'th term from the vocabulary, then the likelihood of a document given a class C is given by[8]

$p(F_1, \dots, F_n \vert C) = \prod_{i=1}^n \left[ F_i\, p(w_i \vert C) + (1 - F_i)(1 - p(w_i \vert C)) \right]$

where $p(w_i \vert C)$ is the probability of class C generating the term $w_i$. 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 naive 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:[10]

Given a collection $D = L \uplus U$ of labeled samples L and unlabeled samples U, start by training a naive Bayes classifier on L.
Until convergence, do:
Predict class probabilities $P(C|x)$ for all examples x in $D$.
Re-train the model based on the probabilities (not the labels) predicted in the previous step.

Convergence is determined based on improvement to the model likelihood $P(D|\theta)$, where $\theta$ denotes the parameters of the naive Bayes model.

This training algorithm is an instance of the more general expectation–maximization algorithm (EM): the prediction step inside the loop is the E-step of EM, while the re-training of naive 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.[10]

## 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,[11] 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.[4] 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 (indicator or frequency features for discrete events), naive Bayes classifiers form a generative-discriminative pair with (multinomial) logistic regression classifiers: each naive Bayes classifier can be considered a way of fitting a probability model p(x, y) to optimize the joint likelihood p(x, y), while logistic regression fits the same probability model to optimize the conditional p(x | y).[12]

The link between the two can be seen by observing that the decision function for naive Bayes (in binary classification) can be rewritten as "predict 1 if the odds of p(y=1 | x) are higher than those of p(y=0 | x)". Expressing this in log-space gives:

$\log\frac{p(y=1|x)}{p(y=0|x)} = \log p(y=1|x) - \log p(y=0|x) > 0$

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 in the discrete case, it can be reparametrised as a linear function $\alpha + \mathbf{\beta}^\top x > 0$. Obtaining the probabilities is then a matter of applying the logistic function to $\alpha + \mathbf{\beta}^\top x$, 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.[12]

## Examples

### Sex 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.

sex height (feet) weight (lbs) foot size(inches)
male 6 180 12
male 5.92 (5'11") 190 11
male 5.58 (5'7") 170 12
male 5.92 (5'11") 165 10
female 5 100 6
female 5.5 (5'6") 150 8
female 5.42 (5'5") 130 7
female 5.75 (5'9") 150 9

The classifier created from the training set using a Gaussian distribution assumption would be (given variances are sample variances):

sex mean (height) variance (height) mean (weight) variance (weight) mean (foot size) variance (foot size)
male 5.855 3.5033e-02 176.25 1.2292e+02 11.25 9.1667e-01
female 5.4175 9.7225e-02 132.5 5.5833e+02 7.5 1.6667e+00

Let's say we have equiprobable classes so P(male)= P(female) = 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 a male or female.

sex height (feet) weight (lbs) foot size(inches)
sample 6 130 8

We wish to determine which posterior is greater, male or female. For the classification as male the posterior is given by

$posterior (male) = \frac{P(male) \, p(height | male) \, p(weight | male) \, p(foot size | male)}{evidence}$

For the classification as female the posterior is given by

$posterior (female) = \frac{P(female) \, p(height | female) \, p(weight | female) \, p(foot size | female)}{evidence}$

The evidence (also termed normalizing constant) may be calculated:

$evidence = P(male) \, p(height | male) \, p(weight | male) \, p(foot size | male)$
$+ P(female) \, p(height | female) \, p(weight | female) \, p(foot size | female)$

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.

$P(male) = 0.5$
$p(\mbox{height} | \mbox{male}) = \frac{1}{\sqrt{2\pi \sigma^2}}\exp\left(\frac{-(6-\mu)^2}{2\sigma^2}\right) \approx 1.5789$,

where $\mu = 5.855$ and $\sigma^2 = 3.5033 \cdot 10^{-2}$ 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.

$p(\mbox{weight} | \mbox{male}) = 5.9881 \cdot 10^{-6}$
$p(\mbox{foot size} | \mbox{male}) = 1.3112 \cdot 10^{-3}$
$\mbox{posterior numerator (male)} = \mbox{their product} = 6.1984 \cdot 10^{-9}$
$P(\mbox{female}) = 0.5$
$p(\mbox{height} | \mbox{female}) = 2.2346 \cdot 10^{-1}$
$p(\mbox{weight} | \mbox{female}) = 1.6789 \cdot 10^{-2}$
$p(\mbox{foot size} | \mbox{female}) = 2.8669 \cdot 10^{-1}$
$\mbox{posterior numerator (female)} = \mbox{their product} = 5.3778 \cdot 10^{-4}$

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 modelled as sets of words where the (independent) probability that the i-th word of a given document occurs in a document from class C can be written as

$p(w_i \vert C)\,$

(For this treatment, we simplify things further by assuming that words are randomly distributed in the document - that is, words are not dependent on the length of the document, position within the document with relation to other words, or other document-context.)

Then the probability that a given document D contains all of the words $w_i$, given a class C, is

$p(D\vert C)=\prod_i p(w_i \vert C)\,$

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 $p(C \vert D)\,$?

$p(D\vert C)={p(D\cap C)\over p(C)}$

and

$p(C\vert D)={p(D\cap C)\over p(D)}$

Bayes' theorem manipulates these into a statement of probability in terms of likelihood.

$p(C\vert D)={p(C)\over p(D)}\,p(D\vert C)$

Assume for the moment that there are only two mutually exclusive classes, S and ¬S (e.g. spam and not spam), such that every element (email) is in either one or the other;

$p(D\vert S)=\prod_i p(w_i \vert S)\,$

and

$p(D\vert\neg S)=\prod_i p(w_i\vert\neg S)\,$

Using the Bayesian result above, we can write:

$p(S\vert D)={p(S)\over p(D)}\,\prod_i p(w_i \vert S)$
$p(\neg S\vert D)={p(\neg S)\over p(D)}\,\prod_i p(w_i \vert\neg S)$

Dividing one by the other gives:

${p(S\vert D)\over p(\neg S\vert D)}={p(S)\,\prod_i p(w_i \vert S)\over p(\neg S)\,\prod_i p(w_i \vert\neg S)}$

Which can be re-factored as:

${p(S\vert D)\over p(\neg S\vert D)}={p(S)\over p(\neg S)}\,\prod_i {p(w_i \vert S)\over p(w_i \vert\neg S)}$

Thus, the probability ratio p(S | D) / p(¬S | D) can be expressed in terms of a series of likelihood ratios. The actual probability p(S | D) can be easily computed from log (p(S | D) / p(¬S | D)) based on the observation that p(S | D) + p(¬S | D) = 1.

Taking the logarithm of all these ratios, we have:

$\ln{p(S\vert D)\over p(\neg S\vert D)}=\ln{p(S)\over p(\neg S)}+\sum_i \ln{p(w_i\vert S)\over p(w_i\vert\neg S)}$

(This technique of "log-likelihood ratios" is a common technique in statistics. In the case of two mutually exclusive alternatives (such as this example), the conversion of a log-likelihood ratio to a probability takes the form of a sigmoid curve: see logit for details.)

Finally, the document can be classified as follows. It is spam if $p(S\vert D) > p(\neg S\vert D)$ (i.e., $\ln{p(S\vert D)\over p(\neg S\vert D)} > 0$), otherwise it is not spam.

## References

1. ^ a b c Hand, D. J.; Yu, K. (2001). "Idiot's Bayes — not so stupid after all?". International Statistical Review 69 (3): 385–399. doi:10.2307/1403452. ISSN 0306-7734.
2. ^ a b c Russell, Stuart; Norvig, Peter (2003) [1995]. Artificial Intelligence: A Modern Approach (2nd ed.). Prentice Hall. ISBN 978-0137903955.
3. ^ a b c Rennie, J.; Shih, L.; Teevan, J.; Karger, D. (2003). "Tackling the poor assumptions of Naive Bayes classifiers". ICML.
4. ^ a b Rish, Irina (2001). "An empirical study of the naive Bayes classifier". IJCAI Workshop on Empirical Methods in AI.
5. ^ Zhang, Harry. "The Optimality of Naive Bayes". FLAIRS2004 conference.
6. ^ Caruana, R.; Niculescu-Mizil, A. (2006). "An empirical comparison of supervised learning algorithms". "Proceedings of the 23rd international conference on Machine learning". CiteSeerX: 10.1.1.122.5901.
7. ^ John, George H.; Langley, Pat (1995). "Estimating Continuous Distributions in Bayesian Classifiers". Proc. Eleventh Conf. on Uncertainty in Artificial Intelligence. Morgan Kaufmann. pp. 338–345.
8. ^ a b c McCallum, Andrew; Nigam, Kamal (1998). "A comparison of event models for Naive Bayes text classification". AAAI-98 workshop on learning for text categorization 752.
9. ^ Metsis, Vangelis; Androutsopoulos, Ion; Paliouras, Georgios (2006). "Spam filtering with Naive Bayes—which Naive Bayes?". Third conference on email and anti-spam (CEAS) 17.
10. ^ a b Nigam, Kamal; McCallum, Andrew; Thrun, Sebastian; Mitchell, Tom (2000). "Learning to classify text from labeled and unlabeled documents using EM". Machine Learning.
11. ^ Niculescu-Mizil, Alexandru; Caruana, Rich (2005). "Predicting good probabilities with supervised learning". ICML. doi:10.1145/1102351.1102430.
12. ^ a b