# Naive Bayes

## Contents

## Naive Bayes

A ** Bayes classifier** is a simple probabilistic Classifier(mathematical) based on applying Bayes' theorem with strong (naive) statistical independence assumptions. A more descriptive term for the underlying probability model would be statistical independence feature model".

In simple terms, a naive Bayes classifier assumes that the presence (or absence) of a particular feature of a class is unrelated to the presence (or absence) of any other feature. For example, a fruit may be considered to be an apple if it is red, round, and about 4" in diameter. Even if these features depend on each other or upon the existence of the other features, a naive Bayes classifier considers all of these properties to independently contribute to the probability that this fruit is an apple.

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.

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]

- [math]= p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ p(F_3\vert C, F_1, F_2) \ \dots p(F_n\vert C, F_1, F_2, F_3,\dots,F_{n-1}).[/math]

Now the "naive" conditional independence assumptions come into play: assume that each feature [math]F_i[/math] is conditionally statistical independence 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]

for [math]i\ne j[/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] (the evidence) 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 each [math]p(F_i\vert C=c)[/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 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 classification and 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. A class' prior may be calculated by assuming equiprobable classes (i.e., priors = 1 / (number of classes)), or by calculating an estimate for the class probability from the training set (i.e., (prior for a given class) = (number of samples in the class) / (total number of samples)). To estimate the parameters for a feature's distribution, one must assume a distribution or generate nonparametric models for the features from the training set. ^{[1]} If one is dealing with continuous data, a typical assumption is the Gaussian distribution, with model parameters of mean and variance.

The mean, [math] \mu [/math], is calculated by

- [math] \mu = \frac 1N \sum_{i=1}^N x_i [/math],

where [math] N [/math] is the number of samples and [math] x_i [/math] is the value of a given sample.

The variance, [math] \sigma^2 [/math], is calculated by

- [math] \sigma^2 = \frac {1}{(N-1)} \sum_{i=1}^N \left(x_i - \mu \right)^2 \,[/math].

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.

## 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) = \underset{c}{\operatorname{argmax}} \ p(C=c) \displaystyle\prod_{i=1}^n p(F_i=f_i\vert C=c).[/math]

## Discussion

Despite the fact that the far-reaching independence assumptions are often inaccurate, the naive Bayes classifier has several properties that make it surprisingly useful in practice. In particular, the decoupling of the class conditional feature distributions means that each distribution can be independently estimated as a one dimensional distribution. This 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.

## R-CARET

**Cite error: <ref> tags exist, but no <references/> tag was found**