A naive Bayes classifier is a simple probabilistic classifier based on applying Bayes' theorem with strong (naive) independence assumptions. A more descriptive term for the underlying probability model would be "independent feature model". An overview of statistical classifiers is given in the article on Pattern recognition.
Introduction[edit]
In simple terms, a naive Bayes classifier assumes that the presence or absence 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.[1] 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.[2]
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[edit]
Abstractly, the probability model for a classifier is a conditional model.
![p(C \vert F_1,\dots,F_n)\,](http://upload.wikimedia.org/math/4/7/a/47a22e381fe04c90c2115d4ac369f590.png)
over a dependent class variable
with a small number of outcomes or classes, conditional on several feature variables
through
. The problem is that if the number of features
is large or when 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)}. \,](http://upload.wikimedia.org/math/3/1/7/3174021f44ba0d31f6ede772624c5523.png)
In plain English the above equation can be written as
![\mbox{posterior} = \frac{\mbox{prior} \times \mbox{likelihood}}{\mbox{evidence}}. \,](http://upload.wikimedia.org/math/b/7/e/b7ee688183182869d44cfaa3b22f5eb6.png)
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
![p(C, F_1, \dots, F_n)\,](http://upload.wikimedia.org/math/0/4/4/044dfd3e1b822193af59618fa30640b2.png)
which can be rewritten as follows, using the chain rule for repeated applications of the definition of conditional probability:
![p(C, F_1, \dots, F_n)\,](http://upload.wikimedia.org/math/0/4/4/044dfd3e1b822193af59618fa30640b2.png)
-
![= p(C) \ p(F_1,\dots,F_n\vert C)](http://upload.wikimedia.org/math/f/9/a/f9afa278bebc99dcf98d0c128c0bc4f6.png)
-
![= p(C) \ p(F_1\vert C) \ p(F_2,\dots,F_n\vert C, F_1)](http://upload.wikimedia.org/math/8/4/4/84456edba2adc600eba6a60630263fee.png)
-
![= 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)](http://upload.wikimedia.org/math/7/c/6/7c65e02bf0e3ea028db57b2cd36fb47d.png)
-
![= p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ p(F_3\vert C, F_1, F_2) \ p(F_4,\dots,F_n\vert C, F_1, F_2, F_3)](http://upload.wikimedia.org/math/4/d/0/4d040737ebbea0caace384f9d7c8f3b5.png)
-
![= 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}).](http://upload.wikimedia.org/math/3/4/b/34b6aa6f4080ea665830249c8edbb29b.png)
Now the "naive" conditional independence assumptions come into play: assume that each feature
is conditionallyindependent of every other feature
for
given the category
. This means that
,
,
, and so on,
for
, and so 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}](http://upload.wikimedia.org/math/6/8/7/6873e9e760aedceeba1583a40948483b.png)
This means that under the above independence assumptions, the conditional distribution over the class variable
is:
![p(C \vert F_1,\dots,F_n) = \frac{1}{Z} p(C) \prod_{i=1}^n p(F_i \vert C)](http://upload.wikimedia.org/math/b/a/e/bae76a0ac40ee656990387fe8f79d0bf.png)
where
(the evidence) is a scaling factor dependent only on
, that is, a constant if the values of the feature variables are known.
Models of this form are much more manageable, because they factor into a so-called class prior
and independent probability distributions
. If there are
classes and if a model for each
can be expressed in terms of
parameters, then the corresponding naive Bayes model has (k â 1) + n r k parameters. In practice, often
(binary classification) and
(Bernoulli variables as features) are common, and so the total number of parameters of the naive Bayes model is
, where
is the number of binary features used for classification.
Parameter estimation and event models[edit]
All model parameters (i.e., class priors and feature probability distributions) can be approximated with relative frequencies from the training set. These are maximum likelihood estimates of the probabilities. 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 generatenonparametric models for the features from the training set.[3]
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.[4][5] When dealing with continuous data, a typical assumption is that the continuous values associated with each class are distributed according to aGaussian distribution.
For example, suppose the training data contain 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 c, and let
be the variance of the values in
associated with class c. Then, the probability density of some value given a class,
, can be computed by plugging
into the equation for a Normal distribution parameterized by
and
. That is,
![P(x=v|c)=\tfrac{1}{\sqrt{2\pi\sigma^2_c}}\,e^{ -\frac{(v-\mu_c)^2}{2\sigma^2_c} }](http://upload.wikimedia.org/math/e/f/6/ef65a6d71f02b376dd42b54ce46abf6e.png)
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. In general, the distribution method is a better choice if there is a small amount of training data, or if the precise distribution of the data is known. The discretization method tends to do better if there is a large amount of training data because it will learn to fit the distribution of the data. Since naive Bayes is typically used when a large amount of data is available (as more computationally expensive models can generally achieve better accuracy), the discretization method is generally preferred over the distribution method.
Sample correction[edit]
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.
Constructing a classifier from the probability model[edit]
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
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).](http://upload.wikimedia.org/math/8/c/8/8c8042557da1dac440a3739a915babd8.png)
Discussion[edit]
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.[6] 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.
Examples[edit]
Sex classification[edit]
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[edit]
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[edit]
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}](http://upload.wikimedia.org/math/5/3/b/53baea12a76b900e655573908f796540.png)
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}](http://upload.wikimedia.org/math/5/b/e/5be62a59b1c25c48b1a884de1c135c63.png)
The evidence (also termed normalizing constant) may be calculated:
![evidence = P(male) \, p(height | male) \, p(weight | male) \, p(foot size | male)](http://upload.wikimedia.org/math/3/e/1/3e1bf31b20d4bca27909dd0ede7a8f19.png)
![+ P(female) \, p(height | female) \, p(weight | female) \, p(foot size | female)](http://upload.wikimedia.org/math/8/f/0/8f0467441e921599c62e6c4ae676927d.png)
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](http://upload.wikimedia.org/math/1/9/5/19527f564d75f29bd5d2bb8db5a7456f.png)
,
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.
![p(\mbox{weight} | \mbox{male}) = 5.9881e-06](http://upload.wikimedia.org/math/5/e/d/5ed08462bc412ec31e4295552d9017c8.png)
![p(\mbox{foot size} | \mbox{male}) = 1.3112e-3](http://upload.wikimedia.org/math/0/1/f/01f231a0fe4b6c98fd7edc242c9b874c.png)
![\mbox{posterior numerator (male)} = \mbox{their product} = 6.1984e-09](http://upload.wikimedia.org/math/2/5/9/2598c33fe9c0e3671c2229c10512a8dd.png)
![P(\mbox{female}) = 0.5](http://upload.wikimedia.org/math/c/4/0/c404eef3fd6fc5637fcf0d0976f421f7.png)
![p(\mbox{height} | \mbox{female}) = 2.2346e-1](http://upload.wikimedia.org/math/c/8/9/c897ef19944836ee383acc54646d314d.png)
![p(\mbox{weight} | \mbox{female}) = 1.6789e-2](http://upload.wikimedia.org/math/d/9/8/d98878be5ffbcb681cce057984be1f02.png)
![p(\mbox{foot size} | \mbox{female}) = 2.8669e-1](http://upload.wikimedia.org/math/8/5/a/85a50858dd417dc5c4c5cecff29419fa.png)
![\mbox{posterior numerator (female)} = \mbox{their product} = 5.3778e-04](http://upload.wikimedia.org/math/8/a/5/8a53cb2560bb7fb0e51d64705568797e.png)
Since posterior numerator is greater in the female case, we predict the sample is female.
Document Classification[edit]
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)\,](http://upload.wikimedia.org/math/5/4/f/54f5d05ffaddba35f01b580cff44352d.png)
(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
, given a class C, is
![p(D\vert C)=\prod_i p(w_i \vert C)\,](http://upload.wikimedia.org/math/4/a/4/4a4ed759de3f17f91be5c2b6f900887d.png)
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
![p(D\vert C)={p(D\cap C)\over p(C)}](http://upload.wikimedia.org/math/7/7/b/77b516d530adfad335295ed750946be8.png)
and
![p(C\vert D)={p(D\cap C)\over p(D)}](http://upload.wikimedia.org/math/0/1/6/016d5b5fbf4ebe500da3b3b9cb4298d1.png)
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)](http://upload.wikimedia.org/math/f/8/a/f8a721484a0aafbdf29c9f1fc357de29.png)
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)\,](http://upload.wikimedia.org/math/5/7/e/57e17fd9122ba156997e3a53a41e38d8.png)
and
![p(D\vert\neg S)=\prod_i p(w_i\vert\neg S)\,](http://upload.wikimedia.org/math/d/7/6/d762b98242953612d4877a96e030269b.png)
Using the Bayesian result above, we can write:
![p(S\vert D)={p(S)\over p(D)}\,\prod_i p(w_i \vert S)](http://upload.wikimedia.org/math/9/a/f/9aff3b4958fdb888e5391a8f710a4adc.png)
![p(\neg S\vert D)={p(\neg S)\over p(D)}\,\prod_i p(w_i \vert\neg S)](http://upload.wikimedia.org/math/2/e/2/2e240ac0b44df0296d485c9ceb6ebe13.png)
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)}](http://upload.wikimedia.org/math/9/a/d/9ad1bf98e32748e481c8c8ddb6862eb0.png)
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)}](http://upload.wikimedia.org/math/2/f/e/2fe9c15070644fbb769ada49428ed346.png)
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)}](http://upload.wikimedia.org/math/d/0/a/d0a16f3fc2876536cd6f22a25bd2121d.png)
(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
(i.e.,
), otherwise it is not spam.
See also[edit]
References[edit]
- Jump up^ Zhang, Harry. "The Optimality of Naive Bayes". FLAIRS2004 conference.
- Jump up^ 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.
- Jump up^ George H. John and Pat Langley (1995). Estimating Continuous Distributions in Bayesian Classifiers. Proceedings of the Eleventh Conference on Uncertainty in Artificial Intelligence. pp. 338-345. Morgan Kaufmann, San Mateo.
- Jump up^ McCallum, Andrew, and Kamal Nigam. "A comparison of event models for Naive Bayes text classification." AAAI-98 workshop on learning for text categorization. Vol. 752. 1998.
- Jump up^ Metsis, Vangelis, Ion Androutsopoulos, and Georgios Paliouras. "Spam filtering with Naive Bayesâwhich Naive Bayes?" Third conference on email and anti-spam (CEAS). Vol. 17. 2006.
- Jump up^ An introductory tutorial to classifiers (introducing the basic terms, with numeric example)
Further reading[edit]
- Domingos, Pedro & Michael Pazzani (1997) "On the optimality of the simple Bayesian classifier under zero-one loss".Machine Learning, 29:103â137. (also online at CiteSeer: [1])
- Rish, Irina. (2001). "An empirical study of the naive Bayes classifier". IJCAI 2001 Workshop on Empirical Methods in Artificial Intelligence. (available online: PDF, PostScript)
- Hand, DJ, & Yu, K. (2001). "Idiot's Bayes - not so stupid after all?" International Statistical Review. Vol 69 part 3, pages 385-399. ISSN 0306-7734.
- Webb, G. I., J. Boughton, and Z. Wang (2005). Not So Naive Bayes: Aggregating One-Dependence Estimators. Machine Learning 58(1). Netherlands: Springer, pages 5â24.
- Mozina M, Demsar J, Kattan M, & Zupan B. (2004). "Nomograms for Visualization of Naive Bayesian Classifier". In Proc. of PKDD-2004, pages 337-348. (available online: PDF)
- Maron, M. E. (1961). "Automatic Indexing: An Experimental Inquiry." Journal of the ACM (JACM) 8(3):404â417.(available online: PDF)
- Minsky, M. (1961). "Steps toward Artificial Intelligence." Proceedings of the IRE 49(1):8-30.
- McCallum, A. and Nigam K. "A Comparison of Event Models for Naive Bayes Text Classification". In AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41â48. Technical Report WS-98-05. AAAI Press. 1998. (available online: PDF)
- Rennie J, Shih L, Teevan J, and Karger D. Tackling The Poor Assumptions of Naive Bayes Classifiers. In Proceedings of the Twentieth International Conference on Machine Learning (ICML). 2003. (available online: PDF)
External links[edit]
- Software