fbpx
Wikipedia

Naive Bayes classifier

In statistics, naive Bayes classifiers are a family of linear "probabilistic classifiers" which assumes that the features are conditionally independent, given the target class. The strength (naivity) of this assumption is what gives the classifier its name. These classifiers are among the simplest Bayesian network models.[1]

Example of a naive Bayes classifier depicted as a Bayesian Network

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.

In the statistics literature, naive Bayes models are known under a variety of names, including simple Bayes and independence Bayes.[3] All these names reference the use of Bayes' theorem in the classifier's decision rule, but naive Bayes is not (necessarily) a Bayesian method.[2][3]

Introduction edit

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.

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.[4] 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.[5]

An advantage of naive Bayes is that it only requires a small amount of training data to estimate the parameters necessary for classification.[6]

Probabilistic model edit

Abstractly, naive Bayes is a conditional probability model: it assigns probabilities   for each of the K possible outcomes or classes   given a problem instance to be classified, represented by a vector   encoding some n features (independent variables).[7]

The problem with the above formulation 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. The model must therefore be reformulated 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 "naive" 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 since the denominator   is omitted.

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 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 so as to minimize the probability of misclassification; 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 k as follows:

 
 
Likelihood functions  , Confusion matrix and ROC curve. For the naive Bayes classifier and given that the a priori probabilities   are the same for all classes, then the decision boundary (green line) would be placed on the point where the two probability densities intersect, due to  .

Parameter estimation and event models edit

A class's prior may be calculated by assuming equiprobable classes, i.e.,  , 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.[8]

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.[9][10]

Gaussian naive Bayes edit

When dealing with continuous data, a typical assumption is that the continuous values associated with each class are distributed according to a normal (or Gaussian) distribution. For example, suppose the training data contains a continuous attribute,  . The data is first segmented by the class, and then the mean and variance of   is computed in each class. Let   be the mean of the values in   associated with class  , and let   be the Bessel corrected variance of the values in   associated with class  . Suppose one has collected some observation value  . Then, the probability density of   given a class  , i.e.,  , can be computed by plugging   into the equation for a normal distribution parameterized by   and  . Formally,

 

Another common technique for handling continuous values is to use binning to discretize the feature values and obtain a new set of Bernoulli-distributed features. Some literature suggests that this is required in order to use naive Bayes, but it is not true, as the discretization may throw away discriminative information.[3]

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,[8] can boost the accuracy of the classifier considerably.[11][12]

Multinomial naive Bayes edit

With a multinomial event model, samples (feature vectors) represent the frequencies with which certain events have been generated by a multinomial   where   is the probability that event i occurs (or K such multinomials in the multiclass case). A feature vector   is then a histogram, with   counting the number of times event i 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 (see bag of words assumption). The likelihood of observing a histogram x is given by:

  where  .

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

 

where   and  . Estimating the parameters in log space is advantageous since multiplying a large number of small values can lead to significant rounding error. Applying a log transform reduces the effect of this rounding error.

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

Bernoulli naive Bayes edit

In the multivariate Bernoulli event model, features are independent Booleans (binary variables) describing inputs. Like the multinomial model, this model is popular for document classification tasks,[9] where binary term occurrence features are used rather than term frequencies. If   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   is given by:[9]

 

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 edit

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:[14]

Given a collection   of labeled samples L and unlabeled samples U, start by training a naive Bayes classifier on L.
Until convergence, do:
Predict class probabilities   for all examples x in  .
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  , where   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.[14]

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. While naive Bayes often fails to produce a good estimate for the correct class probabilities,[15] 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 predicted as 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.[16] Other reasons for the observed success of the naive Bayes classifier are discussed in the literature cited below.

Relation to logistic regression edit

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 that optimizes the joint likelihood  , while logistic regression fits the same probability model to optimize the conditional  .[17]

More formally, we have the following:

Theorem — Naive Bayes classifiers on binary features are subsumed by logistic regression classifiers.

Proof

Consider a generic multiclass classification problem, with possible classes  , then the (non-naive) Bayes classifier gives, by Bayes theorem:

 

The naive Bayes classifier gives

 
where
 

This is exactly a logistic regression classifier.

The link between the two can be seen by observing that the decision function for naive Bayes (in the binary case) 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.[17]

Examples edit

Person 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. Although with NB classifier we treat them as independent, they are not in reality.

Training edit

Example training set below.

Person 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 unbiased sample variances):

Person mean (height) variance (height) mean (weight) variance (weight) mean (foot size) variance (foot size)
male 5.855 3.5033 × 10−2 176.25 1.2292 × 102 11.25 9.1667 × 10−1
female 5.4175 9.7225 × 10−2 132.5 5.5833 × 102 7.5 1.6667

The following example assumes equiprobable classes so that P(male)= P(female) = 0.5. This prior probability distribution might be based on prior knowledge of frequencies in the larger population or in the training set.

Testing edit

Below is a sample to be classified as male or female.

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

In order to classify the sample, one has 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 (also termed normalizing constant) 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. The probability distribution for the sex of the sample can now be determined:

 
 ,

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, the prediction is that 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 modeled 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

 

(For this treatment, things are further simplified 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

 

The question that has to be answered 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 (e.g. spam and not spam), such that every element (email) is in either one or the other;

 

and

 

Using the Bayesian result above, one can write:

 
 

Dividing one by the other gives:

 

Which can be re-factored as:

 

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, one obtains:

 

(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

  1. ^ McCallum, Andrew. "Graphical Models, Lecture2: Bayesian Network Representation" (PDF). Archived (PDF) from the original on 2022-10-09. Retrieved 22 October 2019.
  2. ^ a b Russell, Stuart; Norvig, Peter (2003) [1995]. Artificial Intelligence: A Modern Approach (2nd ed.). Prentice Hall. ISBN 978-0137903955.
  3. ^ 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. JSTOR 1403452.
  4. ^ Zhang, Harry. The Optimality of Naive Bayes (PDF). FLAIRS2004 conference.
  5. ^ Caruana, R.; Niculescu-Mizil, A. (2006). An empirical comparison of supervised learning algorithms. Proc. 23rd International Conference on Machine Learning. CiteSeerX 10.1.1.122.5901.
  6. ^ "Why does Naive Bayes work better when the number of features >> sample size compared to more sophisticated ML algorithms?". Cross Validated Stack Exchange. Retrieved 24 January 2023.
  7. ^ Narasimha Murty, M.; Susheela Devi, V. (2011). Pattern Recognition: An Algorithmic Approach. ISBN 978-0857294944.
  8. ^ a b 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. arXiv:1302.4964.
  9. ^ a b c McCallum, Andrew; Nigam, Kamal (1998). A comparison of event models for Naive Bayes text classification (PDF). AAAI-98 workshop on learning for text categorization. Vol. 752. Archived (PDF) from the original on 2022-10-09.
  10. ^ Metsis, Vangelis; Androutsopoulos, Ion; Paliouras, Georgios (2006). Spam filtering with Naive Bayes—which Naive Bayes?. Third conference on email and anti-spam (CEAS). Vol. 17.
  11. ^ Piryonesi, S. Madeh; El-Diraby, Tamer E. (2020-06-01). "Role of Data Analytics in Infrastructure Asset Management: Overcoming Data Size and Quality Problems". Journal of Transportation Engineering, Part B: Pavements. 146 (2): 04020022. doi:10.1061/JPEODX.0000175. S2CID 216485629.
  12. ^ Hastie, Trevor. (2001). The elements of statistical learning : data mining, inference, and prediction : with 200 full-color illustrations. Tibshirani, Robert., Friedman, J. H. (Jerome H.). New York: Springer. ISBN 0-387-95284-5. OCLC 46809224.
  13. ^ a b Rennie, J.; Shih, L.; Teevan, J.; Karger, D. (2003). Tackling the poor assumptions of naive Bayes classifiers (PDF). ICML. Archived (PDF) from the original on 2022-10-09.
  14. ^ a b Nigam, Kamal; McCallum, Andrew; Thrun, Sebastian; Mitchell, Tom (2000). "Learning to classify text from labeled and unlabeled documents using EM" (PDF). Machine Learning. 39 (2/3): 103–134. doi:10.1023/A:1007692713085. S2CID 686980. Archived (PDF) from the original on 2022-10-09.
  15. ^ Niculescu-Mizil, Alexandru; Caruana, Rich (2005). (PDF). ICML. doi:10.1145/1102351.1102430. Archived from the original (PDF) on 2014-03-11. Retrieved 2016-04-24.
  16. ^ Rish, Irina (2001). An empirical study of the naive Bayes classifier (PDF). IJCAI Workshop on Empirical Methods in AI. Archived (PDF) from the original on 2022-10-09.
  17. ^ a b Ng, Andrew Y.; Jordan, Michael I. (2002). On discriminative vs. generative classifiers: A comparison of logistic regression and naive Bayes. NIPS. Vol. 14.

Further reading edit

  • Domingos, Pedro; Pazzani, Michael (1997). "On the optimality of the simple Bayesian classifier under zero-one loss". Machine Learning. 29 (2/3): 103–137. doi:10.1023/A:1007413511361.
  • Webb, G. I.; Boughton, J.; Wang, Z. (2005). "Not So Naive Bayes: Aggregating One-Dependence Estimators". Machine Learning. 58 (1): 5–24. doi:10.1007/s10994-005-4258-6.
  • Mozina, M.; Demsar, J.; Kattan, M.; Zupan, B. (2004). Nomograms for Visualization of Naive Bayesian Classifier (PDF). Proc. PKDD-2004. pp. 337–348.
  • Maron, M. E. (1961). "Automatic Indexing: An Experimental Inquiry". Journal of the ACM. 8 (3): 404–417. doi:10.1145/321075.321084. hdl:2027/uva.x030748531. S2CID 6692916.
  • Minsky, M. (1961). Steps toward Artificial Intelligence. Proc. IRE. Vol. 49. pp. 8–30.

External links edit

  • Book Chapter: Naive Bayes text classification, Introduction to Information Retrieval
  • Naive Bayes for Text Classification with Unbalanced Classes
  • Benchmark results of Naive Bayes implementations
  • Hierarchical Naive Bayes Classifiers for uncertain data (an extension of the Naive Bayes classifier).
Software
  • Naive Bayes classifiers are available in many general-purpose machine learning and NLP packages, including Apache Mahout, Mallet, NLTK, Orange, scikit-learn and Weka.
  • IMSL Numerical Libraries Collections of math and statistical algorithms available in C/C++, Fortran, Java and C#/.NET. Data mining routines in the IMSL Libraries include a Naive Bayes classifier.
  • An interactive Microsoft Excel spreadsheet Naive Bayes implementation using VBA (requires enabled macros) with viewable source code.
  • jBNC - Bayesian Network Classifier Toolbox
  • Statistical Pattern Recognition Toolbox for Matlab.
  • ifile - the first freely available (Naive) Bayesian mail/spam filter
  • NClassifier - NClassifier is a .NET library that supports text classification and text summarization. It is a port of Classifier4J.
  • Classifier4J - Classifier4J is a Java library designed to do text classification. It comes with an implementation of a Bayesian classifier.
  • JNBC Naive Bayes Classifier running in-memory or using fast key-value stores (MapDB, LevelDB or RocksDB).
  • Blayze - Blayze is a minimal JVM library for Naive Bayes classification written in Kotlin.

naive, bayes, classifier, statistics, naive, bayes, classifiers, family, linear, probabilistic, classifiers, which, assumes, that, features, conditionally, independent, given, target, class, strength, naivity, this, assumption, what, gives, classifier, name, t. In statistics naive Bayes classifiers are a family of linear probabilistic classifiers which assumes that the features are conditionally independent given the target class The strength naivity of this assumption is what gives the classifier its name These classifiers are among the simplest Bayesian network models 1 Example of a naive Bayes classifier depicted as a Bayesian NetworkNaive 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 In the statistics literature naive Bayes models are known under a variety of names including simple Bayes and independence Bayes 3 All these names reference the use of Bayes theorem in the classifier s decision rule but naive Bayes is not necessarily a Bayesian method 2 3 Contents 1 Introduction 2 Probabilistic model 2 1 Constructing a classifier from the probability model 3 Parameter estimation and event models 3 1 Gaussian naive Bayes 3 2 Multinomial naive Bayes 3 3 Bernoulli naive Bayes 3 4 Semi supervised parameter estimation 4 Discussion 4 1 Relation to logistic regression 5 Examples 5 1 Person classification 5 1 1 Training 5 1 2 Testing 5 2 Document classification 6 See also 7 References 8 Further reading 9 External linksIntroduction editNaive 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 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 4 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 5 An advantage of naive Bayes is that it only requires a small amount of training data to estimate the parameters necessary for classification 6 Probabilistic model editAbstractly naive Bayes is a conditional probability model it assigns probabilities p C k x 1 x n displaystyle p C k mid x 1 ldots x n nbsp for each of the K possible outcomes or classes C k displaystyle C k nbsp given a problem instance to be classified represented by a vector x x 1 x n displaystyle mathbf x x 1 ldots x n nbsp encoding some n features independent variables 7 The problem with the above formulation 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 The model must therefore be reformulated to make it more tractable Using Bayes theorem the conditional probability can be decomposed as p C k x p C k p x C k p x displaystyle p C k mid mathbf x frac p C k p mathbf x mid C k p mathbf x nbsp In plain English using Bayesian probability terminology the above equation can be written as posterior prior likelihood evidence displaystyle text posterior frac text prior times text likelihood text evidence nbsp In practice there is interest only in the numerator of that fraction because the denominator does not depend on C displaystyle C nbsp and the values of the features x i displaystyle x i nbsp are given so that the denominator is effectively constant The numerator is equivalent to the joint probability model p C k x 1 x n displaystyle p C k x 1 ldots x n nbsp which can be rewritten as follows using the chain rule for repeated applications of the definition of conditional probability p C k x 1 x n p x 1 x n C k p x 1 x 2 x n C k p x 2 x n C k p x 1 x 2 x n C k p x 2 x 3 x n C k p x 3 x n C k p x 1 x 2 x n C k p x 2 x 3 x n C k p x n 1 x n C k p x n C k p C k displaystyle begin aligned p C k x 1 ldots x n amp p x 1 ldots x n C k amp p x 1 mid x 2 ldots x n C k p x 2 ldots x n C k amp p x 1 mid x 2 ldots x n C k p x 2 mid x 3 ldots x n C k p x 3 ldots x n C k amp cdots amp p x 1 mid x 2 ldots x n C k p x 2 mid x 3 ldots x n C k cdots p x n 1 mid x n C k p x n mid C k p C k end aligned nbsp Now the naive conditional independence assumptions come into play assume that all features in x displaystyle mathbf x nbsp are mutually independent conditional on the category C k displaystyle C k nbsp Under this assumption p x i x i 1 x n C k p x i C k displaystyle p x i mid x i 1 ldots x n C k p x i mid C k nbsp Thus the joint model can be expressed as p C k x 1 x n p C k x 1 x n p C k p x 1 C k p x 2 C k p x 3 C k p C k i 1 n p x i C k displaystyle begin aligned p C k mid x 1 ldots x n varpropto amp p C k x 1 ldots x n amp p C k p x 1 mid C k p x 2 mid C k p x 3 mid C k cdots amp p C k prod i 1 n p x i mid C k end aligned nbsp where displaystyle varpropto nbsp denotes proportionality since the denominator p x displaystyle p mathbf x nbsp is omitted This means that under the above independence assumptions the conditional distribution over the class variable C displaystyle C nbsp is p C k x 1 x n 1 Z p C k i 1 n p x i C k displaystyle p C k mid x 1 ldots x n frac 1 Z p C k prod i 1 n p x i mid C k nbsp where the evidence Z p x k p C k p x C k displaystyle Z p mathbf x sum k p C k p mathbf x mid C k nbsp is a scaling factor dependent only on x 1 x n displaystyle x 1 ldots x n nbsp that is a constant if the values of the feature variables are known 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 so as to minimize the probability of misclassification 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 y C k displaystyle hat y C k nbsp for some k as follows y argmax k 1 K p C k i 1 n p x i C k displaystyle hat y underset k in 1 ldots K operatorname argmax p C k displaystyle prod i 1 n p x i mid C k nbsp nbsp Likelihood functions p x Y displaystyle p mathbf x mid Y nbsp Confusion matrix and ROC curve For the naive Bayes classifier and given that the a priori probabilities p Y displaystyle p Y nbsp are the same for all classes then the decision boundary green line would be placed on the point where the two probability densities intersect due to p Y x p Y p x Y p x p x Y displaystyle p Y mid mathbf x frac p Y p mathbf x mid Y p mathbf x propto p mathbf x mid Y nbsp Parameter estimation and event models editA class s prior may be calculated by assuming equiprobable classes i e p C k 1 K displaystyle p C k frac 1 K nbsp or by calculating an estimate for the class probability from the training set prior for a given class no of samples in that class total no of samples displaystyle text prior for a given class frac text no of samples in that class text total no of samples nbsp 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 8 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 9 10 Gaussian naive Bayes edit When dealing with continuous data a typical assumption is that the continuous values associated with each class are distributed according to a normal or Gaussian distribution For example suppose the training data contains a continuous attribute x displaystyle x nbsp The data is first segmented by the class and then the mean and variance of x displaystyle x nbsp is computed in each class Let m k displaystyle mu k nbsp be the mean of the values in x displaystyle x nbsp associated with class C k displaystyle C k nbsp and let s k 2 displaystyle sigma k 2 nbsp be the Bessel corrected variance of the values in x displaystyle x nbsp associated with class C k displaystyle C k nbsp Suppose one has collected some observation value v displaystyle v nbsp Then the probability density of v displaystyle v nbsp given a class C k displaystyle C k nbsp i e p x v C k displaystyle p x v mid C k nbsp can be computed by plugging v displaystyle v nbsp into the equation for a normal distribution parameterized by m k displaystyle mu k nbsp and s k 2 displaystyle sigma k 2 nbsp Formally p x v C k 1 2 p s k 2 e v m k 2 2 s k 2 displaystyle p x v mid C k frac 1 sqrt 2 pi sigma k 2 e frac v mu k 2 2 sigma k 2 nbsp Another common technique for handling continuous values is to use binning to discretize the feature values and obtain a new set of Bernoulli distributed features Some literature suggests that this is required in order to use naive Bayes but it is not true as the discretization may throw away discriminative information 3 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 8 can boost the accuracy of the classifier considerably 11 12 Multinomial naive Bayes edit With a multinomial event model samples feature vectors represent the frequencies with which certain events have been generated by a multinomial p 1 p n displaystyle p 1 dots p n nbsp where p i displaystyle p i nbsp is the probability that event i occurs or K such multinomials in the multiclass case A feature vector x x 1 x n displaystyle mathbf x x 1 dots x n nbsp is then a histogram with x i displaystyle x i nbsp counting the number of times event i 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 see bag of words assumption The likelihood of observing a histogram x is given by p x C k i 1 n x i i 1 n x i i 1 n p k i x i displaystyle p mathbf x mid C k frac sum i 1 n x i prod i 1 n x i prod i 1 n p ki x i nbsp where p k i p x i C k displaystyle p ki p x i mid C k nbsp The multinomial naive Bayes classifier becomes a linear classifier when expressed in log space 13 log p C k x log p C k i 1 n p k i x i log p C k i 1 n x i log p k i b w k x displaystyle begin aligned log p C k mid mathbf x amp varpropto log left p C k prod i 1 n p ki x i right amp log p C k sum i 1 n x i cdot log p ki amp b mathbf w k top mathbf x end aligned nbsp where b log p C k displaystyle b log p C k nbsp and w k i log p k i displaystyle w ki log p ki nbsp Estimating the parameters in log space is advantageous since multiplying a large number of small values can lead to significant rounding error Applying a log transform reduces the effect of this rounding error 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 13 Bernoulli naive Bayes edit In the multivariate Bernoulli event model features are independent Booleans binary variables describing inputs Like the multinomial model this model is popular for document classification tasks 9 where binary term occurrence features are used rather than term frequencies If x i displaystyle x i nbsp 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 k displaystyle C k nbsp is given by 9 p x C k i 1 n p k i x i 1 p k i 1 x i displaystyle p mathbf x mid C k prod i 1 n p ki x i 1 p ki 1 x i nbsp where p k i displaystyle p ki nbsp is the probability of class C k displaystyle C k nbsp generating the term x i displaystyle x i nbsp 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 edit 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 14 Given a collection D L U displaystyle D L uplus U nbsp 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 displaystyle P C mid x nbsp for all examples x in D displaystyle D nbsp Re train the model based on the probabilities not the labels predicted in the previous step dd Convergence is determined based on improvement to the model likelihood P D 8 displaystyle P D mid theta nbsp where 8 displaystyle theta nbsp 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 14 Discussion editDespite 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 15 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 predicted as 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 16 Other reasons for the observed success of the naive Bayes classifier are discussed in the literature cited below Relation to logistic regression edit 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 that optimizes the joint likelihood p C x displaystyle p C mathbf x nbsp while logistic regression fits the same probability model to optimize the conditional p C x displaystyle p C mid mathbf x nbsp 17 More formally we have the following Theorem Naive Bayes classifiers on binary features are subsumed by logistic regression classifiers Proof Consider a generic multiclass classification problem with possible classes Y 1 n displaystyle Y in 1 n nbsp then the non naive Bayes classifier gives by Bayes theorem p Y X x softmax ln p Y k ln p X x Y k k displaystyle p Y mid X x text softmax ln p Y k ln p X x mid Y k k nbsp The naive Bayes classifier givessoftmax ln p Y k 1 2 i a i k a i k x i a i k a i k k displaystyle text softmax left left ln p Y k frac 1 2 sum i a i k a i k x i a i k a i k right k right nbsp where a i s ln p X i 1 Y s a i s ln p X i 1 Y s displaystyle a i s ln p X i 1 mid Y s quad a i s ln p X i 1 mid Y s nbsp This is exactly a logistic regression classifier The link between the two can be seen by observing that the decision function for naive Bayes in the binary case can be rewritten as predict class C 1 displaystyle C 1 nbsp if the odds of p C 1 x displaystyle p C 1 mid mathbf x nbsp exceed those of p C 2 x displaystyle p C 2 mid mathbf x nbsp Expressing this in log space gives log p C 1 x p C 2 x log p C 1 x log p C 2 x gt 0 displaystyle log frac p C 1 mid mathbf x p C 2 mid mathbf x log p C 1 mid mathbf x log p C 2 mid mathbf x gt 0 nbsp 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 b w x gt 0 displaystyle b mathbf w top x gt 0 nbsp Obtaining the probabilities is then a matter of applying the logistic function to b w x displaystyle b mathbf w top x nbsp 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 17 Examples editPerson 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 Although with NB classifier we treat them as independent they are not in reality Training edit Example training set below Person height feet weight lbs foot size inches male 6 180 12male 5 92 5 11 190 11male 5 58 5 7 170 12male 5 92 5 11 165 10female 5 100 6female 5 5 5 6 150 8female 5 42 5 5 130 7female 5 75 5 9 150 9The classifier created from the training set using a Gaussian distribution assumption would be given variances are unbiased sample variances Person mean height variance height mean weight variance weight mean foot size variance foot size male 5 855 3 5033 10 2 176 25 1 2292 102 11 25 9 1667 10 1female 5 4175 9 7225 10 2 132 5 5 5833 102 7 5 1 6667The following example assumes equiprobable classes so that P male P female 0 5 This prior probability distribution might be based on prior knowledge of frequencies in the larger population or in the training set Testing edit Below is a sample to be classified as male or female Person height feet weight lbs foot size inches sample 6 130 8In order to classify the sample one has to determine which posterior is greater male or female For the classification as male the posterior is given by posterior male P male p height male p weight male p foot size male e v i d e n c e displaystyle text posterior male frac P text male p text height mid text male p text weight mid text male p text foot size mid text male evidence nbsp For the classification as female the posterior is given by posterior female P female p height female p weight female p foot size female e v i d e n c e displaystyle text posterior female frac P text female p text height mid text female p text weight mid text female p text foot size mid text female evidence nbsp 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 displaystyle begin aligned text evidence P text male p text height mid text male p text weight mid text male p text foot size mid text male P text female p text height mid text female p text weight mid text female p text foot size mid text female end aligned nbsp 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 The probability distribution for the sex of the sample can now be determined P male 0 5 displaystyle P text male 0 5 nbsp p height male 1 2 p s 2 exp 6 m 2 2 s 2 1 5789 displaystyle p text height mid text male frac 1 sqrt 2 pi sigma 2 exp left frac 6 mu 2 2 sigma 2 right approx 1 5789 nbsp where m 5 855 displaystyle mu 5 855 nbsp and s 2 3 5033 10 2 displaystyle sigma 2 3 5033 cdot 10 2 nbsp 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 weight male 1 2 p s 2 exp 130 m 2 2 s 2 5 9881 10 6 displaystyle p text weight mid text male frac 1 sqrt 2 pi sigma 2 exp left frac 130 mu 2 2 sigma 2 right 5 9881 cdot 10 6 nbsp p foot size male 1 2 p s 2 exp 8 m 2 2 s 2 1 3112 10 3 displaystyle p text foot size mid text male frac 1 sqrt 2 pi sigma 2 exp left frac 8 mu 2 2 sigma 2 right 1 3112 cdot 10 3 nbsp posterior numerator male their product 6 1984 10 9 displaystyle text posterior numerator male text their product 6 1984 cdot 10 9 nbsp P female 0 5 displaystyle P text female 0 5 nbsp p height female 2 23 10 1 displaystyle p text height mid text female 2 23 cdot 10 1 nbsp p weight female 1 6789 10 2 displaystyle p text weight mid text female 1 6789 cdot 10 2 nbsp p foot size female 2 8669 10 1 displaystyle p text foot size mid text female 2 8669 cdot 10 1 nbsp posterior numerator female their product 5 3778 10 4 displaystyle text posterior numerator female text their product 5 3778 cdot 10 4 nbsp Since posterior numerator is greater in the female case the prediction is that 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 modeled 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 C displaystyle p w i mid C nbsp For this treatment things are further simplified 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 displaystyle w i nbsp given a class C is p D C i p w i C displaystyle p D mid C prod i p w i mid C nbsp The question that has to be answered is what is the probability that a given document D belongs to a given class C In other words what is p C D displaystyle p C mid D nbsp Now by definition p D C p D C p C displaystyle p D mid C p D cap C over p C nbsp and p C D p D C p D displaystyle p C mid D p D cap C over p D nbsp Bayes theorem manipulates these into a statement of probability in terms of likelihood p C D p C p D C p D displaystyle p C mid D frac p C p D mid C p D nbsp 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 S i p w i S displaystyle p D mid S prod i p w i mid S nbsp and p D S i p w i S displaystyle p D mid neg S prod i p w i mid neg S nbsp Using the Bayesian result above one can write p S D p S p D i p w i S displaystyle p S mid D p S over p D prod i p w i mid S nbsp p S D p S p D i p w i S displaystyle p neg S mid D p neg S over p D prod i p w i mid neg S nbsp Dividing one by the other gives p S D p S D p S i p w i S p S i p w i S displaystyle p S mid D over p neg S mid D p S prod i p w i mid S over p neg S prod i p w i mid neg S nbsp Which can be re factored as p S D p S D p S p S i p w i S p w i S displaystyle p S mid D over p neg S mid D p S over p neg S prod i p w i mid S over p w i mid neg S nbsp 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 one obtains ln p S D p S D ln p S p S i ln p w i S p w i S displaystyle ln p S mid D over p neg S mid D ln p S over p neg S sum i ln p w i mid S over p w i mid neg S nbsp 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 D gt p S D displaystyle p S mid D gt p neg S mid D nbsp i e ln p S D p S D gt 0 displaystyle ln p S mid D over p neg S mid D gt 0 nbsp otherwise it is not spam See also editAODE Bayes classifier Bayesian spam filtering Bayesian network Random naive Bayes Linear classifier Logistic regression Perceptron Take the best heuristicReferences editThis article includes a list of general references but it lacks sufficient corresponding inline citations Please help to improve this article by introducing more precise citations May 2009 Learn how and when to remove this template message McCallum Andrew Graphical Models Lecture2 Bayesian Network Representation PDF Archived PDF from the original on 2022 10 09 Retrieved 22 October 2019 a b Russell Stuart Norvig Peter 2003 1995 Artificial Intelligence A Modern Approach 2nd ed Prentice Hall ISBN 978 0137903955 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 JSTOR 1403452 Zhang Harry The Optimality of Naive Bayes PDF FLAIRS2004 conference Caruana R Niculescu Mizil A 2006 An empirical comparison of supervised learning algorithms Proc 23rd International Conference on Machine Learning CiteSeerX 10 1 1 122 5901 Why does Naive Bayes work better when the number of features gt gt sample size compared to more sophisticated ML algorithms Cross Validated Stack Exchange Retrieved 24 January 2023 Narasimha Murty M Susheela Devi V 2011 Pattern Recognition An Algorithmic Approach ISBN 978 0857294944 a b 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 arXiv 1302 4964 a b c McCallum Andrew Nigam Kamal 1998 A comparison of event models for Naive Bayes text classification PDF AAAI 98 workshop on learning for text categorization Vol 752 Archived PDF from the original on 2022 10 09 Metsis Vangelis Androutsopoulos Ion Paliouras Georgios 2006 Spam filtering with Naive Bayes which Naive Bayes Third conference on email and anti spam CEAS Vol 17 Piryonesi S Madeh El Diraby Tamer E 2020 06 01 Role of Data Analytics in Infrastructure Asset Management Overcoming Data Size and Quality Problems Journal of Transportation Engineering Part B Pavements 146 2 04020022 doi 10 1061 JPEODX 0000175 S2CID 216485629 Hastie Trevor 2001 The elements of statistical learning data mining inference and prediction with 200 full color illustrations Tibshirani Robert Friedman J H Jerome H New York Springer ISBN 0 387 95284 5 OCLC 46809224 a b Rennie J Shih L Teevan J Karger D 2003 Tackling the poor assumptions of naive Bayes classifiers PDF ICML Archived PDF from the original on 2022 10 09 a b Nigam Kamal McCallum Andrew Thrun Sebastian Mitchell Tom 2000 Learning to classify text from labeled and unlabeled documents using EM PDF Machine Learning 39 2 3 103 134 doi 10 1023 A 1007692713085 S2CID 686980 Archived PDF from the original on 2022 10 09 Niculescu Mizil Alexandru Caruana Rich 2005 Predicting good probabilities with supervised learning PDF ICML doi 10 1145 1102351 1102430 Archived from the original PDF on 2014 03 11 Retrieved 2016 04 24 Rish Irina 2001 An empirical study of the naive Bayes classifier PDF IJCAI Workshop on Empirical Methods in AI Archived PDF from the original on 2022 10 09 a b Ng Andrew Y Jordan Michael I 2002 On discriminative vs generative classifiers A comparison of logistic regression and naive Bayes NIPS Vol 14 Further reading editDomingos Pedro Pazzani Michael 1997 On the optimality of the simple Bayesian classifier under zero one loss Machine Learning 29 2 3 103 137 doi 10 1023 A 1007413511361 Webb G I Boughton J Wang Z 2005 Not So Naive Bayes Aggregating One Dependence Estimators Machine Learning 58 1 5 24 doi 10 1007 s10994 005 4258 6 Mozina M Demsar J Kattan M Zupan B 2004 Nomograms for Visualization of Naive Bayesian Classifier PDF Proc PKDD 2004 pp 337 348 Maron M E 1961 Automatic Indexing An Experimental Inquiry Journal of the ACM 8 3 404 417 doi 10 1145 321075 321084 hdl 2027 uva x030748531 S2CID 6692916 Minsky M 1961 Steps toward Artificial Intelligence Proc IRE Vol 49 pp 8 30 External links editBook Chapter Naive Bayes text classification Introduction to Information Retrieval Naive Bayes for Text Classification with Unbalanced Classes Benchmark results of Naive Bayes implementations Hierarchical Naive Bayes Classifiers for uncertain data an extension of the Naive Bayes classifier SoftwareNaive Bayes classifiers are available in many general purpose machine learning and NLP packages including Apache Mahout Mallet NLTK Orange scikit learn and Weka IMSL Numerical Libraries Collections of math and statistical algorithms available in C C Fortran Java and C NET Data mining routines in the IMSL Libraries include a Naive Bayes classifier An interactive Microsoft Excel spreadsheet Naive Bayes implementation using VBA requires enabled macros with viewable source code jBNC Bayesian Network Classifier Toolbox Statistical Pattern Recognition Toolbox for Matlab ifile the first freely available Naive Bayesian mail spam filter NClassifier NClassifier is a NET library that supports text classification and text summarization It is a port of Classifier4J Classifier4J Classifier4J is a Java library designed to do text classification It comes with an implementation of a Bayesian classifier JNBC Naive Bayes Classifier running in memory or using fast key value stores MapDB LevelDB or RocksDB Blayze Blayze is a minimal JVM library for Naive Bayes classification written in Kotlin Retrieved from https en wikipedia org w index php title Naive Bayes classifier amp oldid 1190753954, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.