# Naive Bayes

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

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

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

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

In plain English the above equation can be written as

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

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

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

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

$p(C, F_1, \dots, F_n)\,$
$= p(C) \ p(F_1,\dots,F_n\vert C)$
$= p(C) \ p(F_1\vert C) \ p(F_2,\dots,F_n\vert C, F_1)$
$= p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ p(F_3,\dots,F_n\vert C, F_1, F_2)$
$= p(C) \ p(F_1\vert C) \ p(F_2\vert C, F_1) \ p(F_3\vert C, F_1, F_2) \ p(F_4,\dots,F_n\vert C, F_1, F_2, F_3)$
$= 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}).$

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

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

for $i\ne j$, and so the joint model can be expressed as

$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\,$
$= p(C) \prod_{i=1}^n p(F_i \vert C).\,$

This means that under the above independence assumptions, the conditional distribution over the class variable $C$ can be expressed like this:

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

where $Z$ (the evidence) is a scaling factor dependent only on $F_1,\dots,F_n$, 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 $p(C)$ and independent probability distributions $p(F_i\vert C)$. If there are $k$ classes and if a model for each $p(F_i\vert C=c)$ can be expressed in terms of $r$ parameters, then the corresponding naive Bayes model has (k − 1) + n r k parameters. In practice, often $k=2$ (binary classification) and $r=1$ (Bernoulli variables as features) are common, and so the total number of parameters of the naive Bayes model is $2n+1$, where $n$ 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, $\mu$, is calculated by

$\mu = \frac 1N \sum_{i=1}^N x_i$,

where $N$ is the number of samples and $x_i$ is the value of a given sample.

The variance, $\sigma^2$, is calculated by

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

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 $\mathrm{classify}$ defined as follows:

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

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