Naive Bayes classifier
87339
224859695
2008-07-10T19:01:48Z
12.44.178.253
A '''naive Bayes classifier''' is a simple probabilistic [[Classifier (mathematics)|classifier]] based on applying [[Bayes' theorem]] with strong (naive) [[statistical independence|independence]] assumptions. A more descriptive term for the underlying probability model would be "[[statistical independence|independent]] feature model".
In simple terms, a naive Bayes classifier assumes that the presence (or lack of presence) of a particular feature of a class is unrelated to the presence (or lack of presence) of any other feature. For example, a fruit may be considered to be an apple if it is red, round and about 2" in diameter (these features depend on the existence of the others). To a naive Bayes classifier all of these properties are considered to independently contribute to the probability that this fruit is an apple (a naive Bayes classifier can still compute the probability that this object is an apple even if some of the features are not present).
Depending on the precise nature of the probability model, 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 believing in [[Bayesian probability]] or using any Bayesian methods.
In spite of their naive design and apparently over-simplified assumptions, naive Bayes classifiers often work much better in many complex real-world situations than one might expect. Recently, careful analysis of the Bayesian classification problem has shown that there are some theoretical reasons for the apparently unreasonable [[efficacy]] of naive Bayes classifiers ([http://www.cs.unb.ca/profs/hzhang/publications/FLAIRS04ZhangH.pdf Zhang04]). An advantage of the naive Bayes classifier is that it 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.
== The naive Bayes probabilistic model ==
Abstractly, the probability model for a classifier is a conditional model
:<math>p(C \vert F_1,\dots,F_n)\,</math>
over a dependent class variable <math>C</math> with a small number of outcomes or ''classes'', conditional on several feature variables <math>F_1</math> through <math>F_n</math>. The problem is that if the number of features <math>n</math> 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]], we write
:<math>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)}. \,</math>
In plain English the above equation can be written as
:<math>\mbox{posterior} = \frac{\mbox{prior} \times \mbox{likelihood}}{\mbox{evidence}}. \,</math>
In practice we are only interested in the numerator of that fraction, since the denominator does not depend on <math>C</math> and the values of the features <math>F_i</math> are given, so that the denominator is effectively constant.
The numerator is equivalent to the [[joint probability]] model
:<math>p(C, F_1, \dots, F_n)\,</math>
which can be rewritten as follows, using repeated applications of the definition of [[conditional probability]]:
:<math>p(C, F_1, \dots, F_n)\,</math>
::<math>= p(C) \ p(F_1,\dots,F_n\vert C)</math>
::<math>= p(C) \ p(F_1\vert C) \ p(F_2,\dots,F_n\vert C, F_1)</math>
::<math>= 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)</math>
::<math>= 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)</math>
and so forth. Now the "naive" [[conditional independence]] assumptions come into play: assume that each feature <math>F_i</math> is conditionally [[statistical independence|independent]] of every other feature <math>F_j</math> for <math>j\neq i</math>. This means that
:<math>p(F_i \vert C, F_j) = p(F_i \vert C)\,</math>
and so the joint model can be expressed as
:<math>p(C, F_1, \dots, F_n)
= p(C) \ p(F_1\vert C) \ p(F_2\vert C) \ p(F_3\vert C) \ \cdots\,</math>
:<math>= p(C) \prod_{i=1}^n p(F_i \vert C).\,</math>
This means that under the above independence assumptions, the conditional distribution over the class variable <math>C</math> can be expressed like this:
:<math>p(C \vert F_1,\dots,F_n) = \frac{1}{Z} p(C) \prod_{i=1}^n p(F_i \vert C)</math>
where <math>Z</math> is a scaling factor dependent only on <math>F_1,\dots,F_n</math>, i.e., a constant if the values of the feature variables are known.
Models of this form are much more manageable, since they factor into a so-called ''class prior'' <math>p(C)</math> and independent probability distributions <math>p(F_i\vert C)</math>. If there are <math>k</math> classes and if a model for <math>p(F_i)</math> can be expressed in terms of <math>r</math> parameters, then the corresponding naive Bayes model has (''k'' − 1) + ''n'' ''r'' ''k'' parameters. In practice, often <math>k=2</math> (binary classification) and <math>r=1</math> ([[Bernoulli distribution|Bernoulli variables]] as features) are common, and so the total number of parameters of the naive Bayes model is <math>2n+1</math>, where <math>n</math> is the number of binary features used for prediction.
== Parameter estimation ==
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. Non-discrete features need to be [[discretization|discretized]] first. Discretization can be [[unsupervised discretization|unsupervised]] (ad-hoc selection of bins) or [[supervised discretization|supervised]] (binning guided by information in training data).
If a given class and feature value never occur together in the training set then the frequency-based probability estimate will be zero. This is problematic since it will wipe out all information in the other probabilities when they are multiplied. It is therefore often desirable to incorporate a small-sample correction in all probability estimates such that [[Pseudocount|no probability is ever set to be exactly zero]].
== 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 is the function <math>\mathrm{classify}</math> defined as follows:
:<math>\mathrm{classify}(f_1,\dots,f_n) = \mathop{\mathrm{argmax}}_c \ p(C=c) \prod_{i=1}^n p(F_i=f_i\vert C=c).</math>
== Discussion ==
One should notice that the independence assumption may lead to some unexpected results in the calculation of posteriori probability. In some circumstances when there is a dependency between observations, the above-mentioned probability may contradict with the second axiom of probability by which any probability must be less than or equal to one.
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 in turn helps to alleviate problems stemming from the [[curse of dimensionality]], such as the need for data sets that scale exponentially with the number of features. Like all probabilistic classifiers under the MAP decision rule, it arrives at the correct classification as long as the correct class is more probable than any other class; hence class probabilities do not have to be estimated very well. In other words, the overall classifier is 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.
== Example: 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 [[spamming|spam]] and non-spam [[E-mail]]s. 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
:<math>p(w_i \vert C)\,</math>
(For this treatment, we simplify things further by assuming that the probability of a word in a document is independent of the length of a document, or that all documents are of the same length.)
Then the probability of a given document ''D'', given a class ''C'', is
:<math>p(D\vert C)=\prod_i p(w_i \vert C)\,</math>
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 <math>p(C \vert D)\,</math>?
Now, by their definition, (see [[Probability axiom]])
:<math>p(D\vert C)={p(D\cap C)\over p(C)}</math>
and
:<math>p(C\vert D)={p(D\cap C)\over p(D)}</math>
Bayes' theorem manipulates these into a statement of probability in terms of [[likelihood]].
:<math>p(C\vert D)={p(C)\over p(D)}\,p(D\vert C)</math>
Assume for the moment that there are only two classes, ''S'' and ¬''S'' (e.g. spam and not spam).
:<math>p(D\vert S)=\prod_i p(w_i \vert S)\,</math>
and
:<math>p(D\vert\neg S)=\prod_i p(w_i\vert\neg S)\,</math>
Using the Bayesian result above, we can write:
:<math>p(S\vert D)={p(S)\over p(D)}\,\prod_i p(w_i \vert S)</math>
:<math>p(\neg S\vert D)={p(\neg S)\over p(D)}\,\prod_i p(w_i \vert\neg S)</math>
Dividing one by the other gives:
:<math>{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)}</math>
Which can be re-factored as:
:<math>{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)}</math>
Thus, the probability ratio p(''S'' | ''D'') / p(¬''S'' | ''D'') can be expressed in terms of a series of [[likelihood ratio]]s.
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:
:<math>\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)}</math>
(This technique of "[[log-likelihood ratio]]s" 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 <math>\ln{p(S\vert D)\over p(\neg S\vert D)} > 0</math>, otherwise it is not spam.
== See also ==
* [[Bayesian network]]
* [[Linear classifier]]
* [[Bayesian inference]] (esp. as Bayesian techniques relate to [[Spam (e-mail)|spam]])
* [[Boosting]]
* [[Fuzzy logic]]
* [[Logistic regression]]
* [[Neural network]]s
* [[Predictive analytics]]
* [[Perceptron]]
* [[Support vector machine]]
==References==
* 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 [http://citeseer.ist.psu.edu/ CiteSeer]: [http://citeseer.ist.psu.edu/domingos97optimality.html])''
* Rish, Irina. (2001). "An empirical study of the naive Bayes classifier". IJCAI 2001 Workshop on Empirical Methods in Artificial Intelligence. ''(available online: [http://www.research.ibm.com/people/r/rish/papers/RC22230.pdf PDF], [http://www.research.ibm.com/people/r/rish/papers/ijcai-ws.ps 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.
* 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: [http://www.ailab.si/blaz/papers/2004-PKDD.pdf PDF])''
* Maron, M. E. (1961). "Automatic Indexing: An Experimental Inquiry." Journal of the ACM (JACM) 8(3):404–417. ''(available online: [http://delivery.acm.org/10.1145/330000/321084/p404-maron.pdf?key1=321084&key2=9636178211&coll=GUIDE&dl=ACM&CFID=56729577&CFTOKEN=37855803 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: [http://www.kamalnigam.com/papers/multinomial-aaaiws98.pdf PDF])''
* Harry Zhang "The Optimality of Naive Bayes". (available online: [http://www.cs.unb.ca/profs/hzhang/publications/FLAIRS04ZhangH.pdf PDF])''
* S.Kotsiantis, P. Pintelas, Increasing the Classification Accuracy of Simple Bayesian Classifier, Lecture Notes in Artificial Intelligence, AIMSA 2004, Springer-Verlag Vol 3192, pp. 198-207, 2004 (http://www.math.upatras.gr/~esdlab/en/members/kotsiantis/improvingNB.pdf)
* S. Kotsiantis, P. Pintelas, Logitboost of Simple Bayesian Classifier, Computational Intelligence in Data mining Special Issue of the Informatica Journal, Vol 29 (1), pp. 53-59, 2005 (http://www.math.upatras.gr/~esdlab/en/members/kotsiantis/05_Kotsiantis-Logitboost%20of%20simble%20bayesian..._No%205.pdf)
==External links==
* [http://citeseer.ist.psu.edu/30545.html Naive Bayesian learning]
* [http://www.biomedcentral.com/1471-2105/7/514 Hierarchical Naive Bayes Classifiers for uncertain data] (an extension of the Naive Bayes classifier).
* [http://www.convo.co.uk/x02/ Online application of a Naive Bayes classifier] (emotion modelling), with a full explanation.
* [http://www.bayesnews.xpg.com.br/ BayesNews - Bayesian RSS Reader] (useful for personal news clipping).
;Software
* [http://paul.luminos.nl/documents/show_document.php?d=198 Naive Bayes implementation in Visual Basic] (includes executable and source code).
* An interactive [[Microsoft Excel]] spreadsheet [http://downloads.sourceforge.net/naivebayesclass/NaiveBayesDemo.xls?use_mirror=osdn Naive Bayes implementation] using [[Visual Basic for Applications|VBA]] (requires enabled macros) with viewable source code.
* [http://jbnc.sourceforge.net/ jBNC - Bayesian Network Classifier Toolbox]
* [http://popfile.sourceforge.net/ POPFile ] Perl-based email proxy system classifies email into user-defined "buckets", including spam.
* [http://cmp.felk.cvut.cz/cmp/software/stprtool/ Statistical Pattern Recognition Toolbox for Matlab].
[[Category:Classification algorithms]]
[[Category:Bayesian statistics]]
[[Category:Statistical classification]]
[[de:Bayes-Klassifikator]]
[[es:Clasificador bayesiano]]
[[ja:単純ベイズ分類器]]
[[pl:Naiwny klasyfikator bayesowski]]
[[ru:Наивный байесовский классификатор]]
[[fr:Classification Naïve Bayesienne]]