Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Naive Bayes classifier

From Wikipedia, the free encyclopedia
Probabilistic classification algorithm
Example of a naive Bayes classifier depicted as a Bayesian Network

Instatistics,naive (sometimessimple oridiot's)Bayes classifiers are a family of "probabilistic classifiers" which assumes that the features are conditionally independent, given the target class.[1] In other words, a naive Bayes model assumes the information about the class provided by each variable is unrelated to the information from the others, with no information shared between the predictors. The highly unrealistic nature of this assumption, called thenaive independence assumption, is what gives the classifier its name. These classifiers are some of the simplestBayesian network models.[2]

Naive Bayes classifiers generally perform worse than more advanced models likelogistic regressions, especially atquantifying uncertainty (with naive Bayes models often producing wildly overconfident probabilities). However, they are highly scalable, requiring only one parameter for each feature or predictor in a learning problem.Maximum-likelihood training can be done by evaluating aclosed-form expression (simply by counting observations in each group),[3]: 718  rather than the expensiveiterative approximation algorithms required by most other models.

Despite the use ofBayes' theorem in the classifier's decision rule, naive Bayes is not (necessarily) aBayesian method, and naive Bayes models can be fit to data using eitherBayesian orfrequentist methods.[1][3]

Introduction

[edit]

Naive Bayes is a simple technique for constructing classifiers: models that assign class labels to problem instances, represented as vectors offeature values, where the class labels are drawn from some finite set. There is not a singlealgorithm 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 isindependent 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 possiblecorrelations between the color, roundness, and diameter features.

In many practical applications, parameter estimation for naive Bayes models uses the method ofmaximum likelihood; in other words, one can work with the naive Bayes model without acceptingBayesian 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 implausibleefficacy 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 asboosted trees orrandom 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 aconditional probability model: it assigns probabilitiesp(Ckx1,,xn){\displaystyle p(C_{k}\mid x_{1},\ldots ,x_{n})} for each of theK possible outcomes orclassesCk{\displaystyle C_{k}} given a problem instance to be classified, represented by a vectorx=(x1,,xn){\displaystyle \mathbf {x} =(x_{1},\ldots ,x_{n})} encoding somen features (independent variables).[7]

The problem with the above formulation is that if the number of featuresn is large or if a feature can take on a large number of values, then basing such a model onprobability tables is infeasible. The model must therefore be reformulated to make it more tractable. UsingBayes' theorem, the conditional probability can be decomposed as:p(Ckx)=p(Ck) p(xCk)p(x){\displaystyle p(C_{k}\mid \mathbf {x} )={\frac {p(C_{k})\ p(\mathbf {x} \mid C_{k})}{p(\mathbf {x} )}}\,}

In plain English, usingBayesian probability terminology, the above equation can be written asposterior=prior×likelihoodevidence{\displaystyle {\text{posterior}}={\frac {{\text{prior}}\times {\text{likelihood}}}{\text{evidence}}}\,}

In practice, there is interest only in the numerator of that fraction, because the denominator does not depend onC{\displaystyle C} and the values of the featuresxi{\displaystyle x_{i}} are given, so that the denominator is effectively constant.The numerator is equivalent to thejoint probability modelp(Ck,x1,,xn){\displaystyle p(C_{k},x_{1},\ldots ,x_{n})\,}which can be rewritten as follows, using thechain rule for repeated applications of the definition ofconditional probability:p(Ck,x1,,xn)=p(x1,,xn,Ck)=p(x1x2,,xn,Ck) p(x2,,xn,Ck)=p(x1x2,,xn,Ck) p(x2x3,,xn,Ck) p(x3,,xn,Ck)==p(x1x2,,xn,Ck) p(x2x3,,xn,Ck)p(xn1xn,Ck) p(xnCk) p(Ck){\displaystyle {\begin{aligned}p(C_{k},x_{1},\ldots ,x_{n})&=p(x_{1},\ldots ,x_{n},C_{k})\\&=p(x_{1}\mid x_{2},\ldots ,x_{n},C_{k})\ p(x_{2},\ldots ,x_{n},C_{k})\\&=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})\\&=\cdots \\&=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}}}

Now the "naive"conditional independence assumptions come into play: assume that all features inx{\displaystyle \mathbf {x} } aremutually independent, conditional on the categoryCk{\displaystyle C_{k}}. Under this assumption,p(xixi+1,,xn,Ck)=p(xiCk).{\displaystyle p(x_{i}\mid x_{i+1},\ldots ,x_{n},C_{k})=p(x_{i}\mid C_{k})\,.}

Thus, the joint model can be expressed asp(Ckx1,,xn) p(Ck,x1,,xn)=p(Ck) p(x1Ck) p(x2Ck) p(x3Ck) =p(Ck)i=1np(xiCk),{\displaystyle {\begin{aligned}p(C_{k}\mid x_{1},\ldots ,x_{n})\varpropto \ &p(C_{k},x_{1},\ldots ,x_{n})\\&=p(C_{k})\ p(x_{1}\mid C_{k})\ p(x_{2}\mid C_{k})\ p(x_{3}\mid C_{k})\ \cdots \\&=p(C_{k})\prod _{i=1}^{n}p(x_{i}\mid C_{k})\,,\end{aligned}}}where{\displaystyle \varpropto } denotesproportionality since the denominatorp(x){\displaystyle p(\mathbf {x} )} is omitted.

This means that under the above independence assumptions, the conditional distribution over the class variableC{\displaystyle C} is:p(Ckx1,,xn)=1Z p(Ck)i=1np(xiCk){\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})}where the evidenceZ=p(x)=kp(Ck) p(xCk){\displaystyle Z=p(\mathbf {x} )=\sum _{k}p(C_{k})\ p(\mathbf {x} \mid C_{k})} is a scaling factor dependent only onx1,,xn{\displaystyle x_{1},\ldots ,x_{n}}, that is, a constant if the values of the feature variables are known.

Often, it is only necessary todiscriminate between classes. In that case, the scaling factor is irrelevant, and it is sufficient to calculate the log-probability up to a factor:lnp(Ckx1,,xn)=lnp(Ck)+i=1nlnp(xiCk)lnZirrelevant{\displaystyle \ln p(C_{k}\mid x_{1},\ldots ,x_{n})=\ln p(C_{k})+\sum _{i=1}^{n}\ln p(x_{i}\mid C_{k})\underbrace {-\ln Z} _{\text{irrelevant}}}The scaling factor is irrelevant, since discrimination subtracts it away:lnp(Ckx1,,xn)p(Clx1,,xn)=(lnp(Ck)+i=1nlnp(xiCk))(lnp(Cl)+i=1nlnp(xiCl)){\displaystyle \ln {\frac {p(C_{k}\mid x_{1},\ldots ,x_{n})}{p(C_{l}\mid x_{1},\ldots ,x_{n})}}=\left(\ln p(C_{k})+\sum _{i=1}^{n}\ln p(x_{i}\mid C_{k})\right)-\left(\ln p(C_{l})+\sum _{i=1}^{n}\ln p(x_{i}\mid C_{l})\right)}There are two benefits of using log-probability. One is that it allows an interpretation in information theory, where log-probabilities are units of information innats. Another is that it avoidsarithmetic underflow.

Constructing a classifier from the probability model

[edit]

The discussion so far has derived the independent feature model, that is, the naive Bayesprobability model. The naive Bayesclassifier combines this model with adecision 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 themaximuma posteriori orMAP decision rule. The corresponding classifier, aBayes classifier, is the function that assigns a class labely^=Ck{\displaystyle {\hat {y}}=C_{k}} for somek as follows:y^=argmaxk{1,,K} p(Ck)i=1np(xiCk).{\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}).}

Likelihood functionsp(xY){\displaystyle p(\mathbf {x} \mid Y)},Confusion matrix andROC curve. For the naive Bayes classifier and given that the a priori probabilitiesp(Y){\displaystyle p(Y)} are the same for all classes, then thedecision boundary (green line) would be placed on the point where the two probability densities intersect, due top(Yx)=p(Y) p(xY)p(x)p(xY){\displaystyle p(Y\mid \mathbf {x} )={\frac {p(Y)\ p(\mathbf {x} \mid Y)}{p(\mathbf {x} )}}\propto p(\mathbf {x} \mid Y)}.

Parameter estimation and event models

[edit]

A class's prior may be calculated by assuming equiprobable classes, i.e.,p(Ck)=1K{\displaystyle p(C_{k})={\frac {1}{K}}}, or by calculating an estimate for the class probability from the training set:prior for a given class=no. of samples in that classtotal no. of samples{\displaystyle {\text{prior for a given class}}={\frac {\text{no. of samples in that class}}{\text{total no. 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.[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 andBernoulli 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 anormal (or Gaussian) distribution. For example, suppose the training data contains a continuous attribute,x{\displaystyle x}. The data is first segmented by the class, and then the mean andvariance ofx{\displaystyle x} is computed in each class. Letμk{\displaystyle \mu _{k}} be the mean of the values inx{\displaystyle x} associated with classCk{\displaystyle C_{k}}, and letσk2{\displaystyle \sigma _{k}^{2}} be theBessel corrected variance of the values inx{\displaystyle x} associated with classCk{\displaystyle C_{k}}. Suppose one has collected some observation valuev{\displaystyle v}. Then, the probabilitydensity ofv{\displaystyle v} given a classCk{\displaystyle C_{k}}, i.e.,p(x=vCk){\displaystyle p(x=v\mid C_{k})}, can be computed by pluggingv{\displaystyle v} into the equation for anormal distribution parameterized byμk{\displaystyle \mu _{k}} andσk2{\displaystyle \sigma _{k}^{2}}. Formally,p(x=vCk)=12πσk2e(vμk)22σk2{\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}}}}}

Another common technique for handling continuous values is to use binning todiscretize 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 maythrow away discriminative information.[1]

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 amultinomial(p1,,pn){\displaystyle (p_{1},\dots ,p_{n})} wherepi{\displaystyle p_{i}} is the probability that eventi occurs (orK such multinomials in the multiclass case). A feature vectorx=(x1,,xn){\displaystyle \mathbf {x} =(x_{1},\dots ,x_{n})} is then ahistogram, withxi{\displaystyle x_{i}} counting the number of times eventi 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 (seebag of words assumption).[13] The likelihood of observing a histogramx is given by:p(xCk)=(i=1nxi)!i=1nxi!i=1npkixi{\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}}}wherepki:=p(iCk){\displaystyle p_{ki}:=p(i\mid C_{k})}.

The multinomial naive Bayes classifier becomes alinear classifier when expressed in log-space:[14]logp(Ckx)log(p(Ck)i=1npkixi)=logp(Ck)+i=1nxilogpki=b+wkx{\displaystyle {\begin{aligned}\log p(C_{k}\mid \mathbf {x} )&\varpropto \log \left(p(C_{k})\prod _{i=1}^{n}{p_{ki}}^{x_{i}}\right)\\&=\log p(C_{k})+\sum _{i=1}^{n}x_{i}\cdot \log p_{ki}\\&=b+\mathbf {w} _{k}^{\top }\mathbf {x} \end{aligned}}}whereb=logp(Ck){\displaystyle b=\log p(C_{k})} andwki=logpki{\displaystyle w_{ki}=\log p_{ki}}. 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, calledpseudocount, in all probability estimates such that no probability is ever set to be exactly zero. This way ofregularizing naive Bayes is calledLaplace smoothing when the pseudocount is one, andLidstone smoothing in the general case.

Rennieet al. discuss problems with the multinomial assumption in the context of document classification and possible ways to alleviate those problems, including the use oftf–idf weights instead of raw term frequencies and document length normalization, to produce a naive Bayes classifier that is competitive withsupport vector machines.[14]

Bernoulli naive Bayes

[edit]

In the multivariateBernoulli event model, features are independentBoolean variables (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. Ifxi{\displaystyle x_{i}} is a Boolean expressing the occurrence or absence of thei'th term from the vocabulary, then the likelihood of a document given a classCk{\displaystyle C_{k}} is given by:[9]p(xCk)=i=1npkixi(1pki)(1xi){\displaystyle p(\mathbf {x} \mid C_{k})=\prod _{i=1}^{n}p_{ki}^{x_{i}}(1-p_{ki})^{(1-x_{i})}}wherepki{\displaystyle p_{ki}} is the probability of classCk{\displaystyle C_{k}} generating the termxi{\displaystyle x_{i}}. 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 asemi-supervised training algorithm that can learn from a combination of labeled and unlabeled data by running the supervised learning algorithm in a loop:[15]

  1. Given a collectionD=LU{\displaystyle D=L\uplus U} of labeled samplesL and unlabeled samplesU, start by training a naive Bayes classifier onL.
  2. Until convergence, do:
    1. Predict class probabilitiesP(Cx){\displaystyle P(C\mid x)} for all examplesx inD{\displaystyle D}.
    2. Re-train the model based on theprobabilities (not the labels) predicted in the previous step.

Convergence is determined based on improvement to the model likelihoodP(Dθ){\displaystyle P(D\mid \theta )}, whereθ{\displaystyle \theta } denotes the parameters of the naive Bayes model.

This training algorithm is an instance of the more generalexpectation–maximization algorithm (EM): the prediction step inside the loop is theE-step of EM, while the re-training of naive Bayes is theM-step. The algorithm is formally justified by the assumption that the data are generated by amixture model, and the components of this mixture model are exactly the classes of the classification problem.[15]

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 thecurse 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,[16] this may not be a requirement for many applications. For example, the naive Bayes classifier will make the correctMAP 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.[17] 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 agenerative-discriminative pair withmultinomial logistic regression classifiers: each naive Bayes classifier can be considered a way of fitting a probability model that optimizes the joint likelihoodp(C,x){\displaystyle p(C,\mathbf {x} )}, while logistic regression fits the same probability model to optimize the conditionalp(Cx){\displaystyle p(C\mid \mathbf {x} )}.[18]

More formally, we have the following:

TheoremNaive Bayes classifiers on binary features are subsumed by logistic regression classifiers.

Proof

Consider a generic multiclass classification problem, with possible classesY{1,...,n}{\displaystyle Y\in \{1,...,n\}}, then the (non-naive) Bayes classifier gives, by Bayes theorem:p(YX=x)=softmax({lnp(Y=k)+lnp(X=xY=k)}k){\displaystyle p(Y\mid X=x)={\text{softmax}}(\{\ln p(Y=k)+\ln p(X=x\mid Y=k)\}_{k})}

The naive Bayes classifier givessoftmax({lnp(Y=k)+12i(ai,k+ai,k)xi+(ai,k++ai,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)}whereai,s+=lnp(Xi=+1Y=s);ai,s=lnp(Xi=1Y=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)}

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 classC1{\displaystyle C_{1}} if theodds ofp(C1x){\displaystyle p(C_{1}\mid \mathbf {x} )} exceed those ofp(C2x){\displaystyle p(C_{2}\mid \mathbf {x} )}". Expressing this in log-space gives:logp(C1x)p(C2x)=logp(C1x)logp(C2x)>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} )>0}

The left-hand side of this equation is the log-odds, orlogit, 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 functionb+wx>0{\displaystyle b+\mathbf {w} ^{\top }x>0}. Obtaining the probabilities is then a matter of applying thelogistic function tob+wx{\displaystyle b+\mathbf {w} ^{\top }x}, or in the multiclass case, thesoftmax function.

Discriminative classifiers have lower asymptotic error than generative ones; however, research byNg andJordan has shown that in some practical cases naive Bayes can outperform logistic regression because it reaches its asymptotic error faster.[18]

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.

Personheight (feet)weight (lbs)foot size (inches)
male618012
male5.92 (5'11")19011
male5.58 (5'7")17012
male5.92 (5'11")16510
female51006
female5.5 (5'6")1508
female5.42 (5'5")1307
female5.75 (5'9")1509

The classifier created from the training set using a Gaussian distribution assumption would be (given variances areunbiasedsample variances):

Personmean (height)variance (height)mean (weight)variance (weight)mean (foot size)variance (foot size)
male5.8553.5033 × 10−2176.2512.29211.259.1667 × 10−1
female5.41759.7225 × 10−2132.55.58337.51.6667

The following example assumes equiprobable classes so that P(male)= P(female) = 0.5. This priorprobability 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.

Personheight (feet)weight (lbs)foot size (inches)
sample61308

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 byposterior (male)=P(male)p(heightmale)p(weightmale)p(foot sizemale)evidence{\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}})}{\text{evidence}}}}

For the classification as female the posterior is given byposterior (female)=P(female)p(heightfemale)p(weightfemale)p(foot sizefemale)evidence{\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}})}{\text{evidence}}}}

The evidence (also termed normalizing constant) may be calculated:evidence=P(male)p(heightmale)p(weightmale)p(foot sizemale)+P(female)p(heightfemale)p(weightfemale)p(foot sizefemale){\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}}}

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. Theprobability distribution for the sex of the sample can now be determined:P(male)=0.5{\displaystyle P({\text{male}})=0.5}p(heightmale)=12πσ2exp((6μ)22σ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,}whereμ=5.855{\displaystyle \mu =5.855} andσ2=3.5033102{\displaystyle \sigma ^{2}=3.5033\cdot 10^{-2}} 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, becauseheight is a continuous variable.

p(weightmale)=12πσ2exp((130μ)22σ2)=5.9881106{\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}}p(foot sizemale)=12πσ2exp((8μ)22σ2)=1.3112103{\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}}posterior numerator (male)=their product=6.1984109{\displaystyle {\text{posterior numerator (male)}}={\text{their product}}=6.1984\cdot 10^{-9}}

P(female)=0.5{\displaystyle P({\text{female}})=0.5}p(heightfemale)=2.23101{\displaystyle p({\text{height}}\mid {\text{female}})=2.23\cdot 10^{-1}}p(weightfemale)=1.6789102{\displaystyle p({\text{weight}}\mid {\text{female}})=1.6789\cdot 10^{-2}}p(foot sizefemale)=2.8669101{\displaystyle p({\text{foot size}}\mid {\text{female}})=2.8669\cdot 10^{-1}}posterior numerator (female)=their product=5.3778104{\displaystyle {\text{posterior numerator (female)}}={\text{their product}}=5.3778\cdot 10^{-4}}

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 thedocument classification problem.Consider the problem of classifying documents by their content, for example intospam and non-spame-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 classC can be written asp(wiC){\displaystyle p(w_{i}\mid C)\,}

(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 documentD contains all of the wordswi{\displaystyle w_{i}}, given a classC, isp(DC)=ip(wiC){\displaystyle p(D\mid C)=\prod _{i}p(w_{i}\mid C)\,}

The question that has to be answered is: "what is the probability that a given documentD belongs to a given classC?" In other words, what isp(CD){\displaystyle p(C\mid D)\,}?

Nowby definitionp(DC)=p(DC)p(C){\displaystyle p(D\mid C)={p(D\cap C) \over p(C)}}andp(CD)=p(DC)p(D){\displaystyle p(C\mid D)={p(D\cap C) \over p(D)}}

Bayes' theorem manipulates these into a statement of probability in terms oflikelihood.p(CD)=p(C)p(DC)p(D){\displaystyle p(C\mid D)={\frac {p(C)\,p(D\mid C)}{p(D)}}}

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(DS)=ip(wiS){\displaystyle p(D\mid S)=\prod _{i}p(w_{i}\mid S)\,}andp(D¬S)=ip(wi¬S){\displaystyle p(D\mid \neg S)=\prod _{i}p(w_{i}\mid \neg S)\,}

Using the Bayesian result above, one can write:p(SD)=p(S)p(D)ip(wiS){\displaystyle p(S\mid D)={p(S) \over p(D)}\,\prod _{i}p(w_{i}\mid S)}p(¬SD)=p(¬S)p(D)ip(wi¬S){\displaystyle p(\neg S\mid D)={p(\neg S) \over p(D)}\,\prod _{i}p(w_{i}\mid \neg S)}

Dividing one by the other gives:p(SD)p(¬SD)=p(S)ip(wiS)p(¬S)ip(wi¬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)}}

Which can be re-factored as:p(SD)p(¬SD)=p(S)p(¬S)ip(wiS)p(wi¬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)}}

Thus, the probability ratio p(S |D) / p(¬S |D) can be expressed in terms of a series oflikelihood 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 thelogarithm of all these ratios, one obtains:lnp(SD)p(¬SD)=lnp(S)p(¬S)+ilnp(wiS)p(wi¬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)}}

(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 asigmoid curve: seelogit for details.)

Finally, the document can be classified as follows. It is spam ifp(SD)>p(¬SD){\displaystyle p(S\mid D)>p(\neg S\mid D)} (i. e.,lnp(SD)p(¬SD)>0{\displaystyle \ln {p(S\mid D) \over p(\neg S\mid D)}>0}), otherwise it is not spam.

Spam filtering

[edit]

Naive Bayes classifiers are a popularstatistical technique ofe-mail filtering. They typically usebag-of-words features to identifyemail spam, an approach commonly used intext classification. Naive Bayes classifiers work by correlating the use of tokens (typically words, or sometimes other things), with spam and non-spam e-mails and then usingBayes' theorem to calculate a probability that an email is or is not spam.

Naive Bayes spam filtering is a baseline technique for dealing with spam that can tailor itself to the email needs of individual users and give lowfalse positive spam detection rates that are generally acceptable to users. Bayesian algorithms were used for email filtering as early as 1996. Although naive Bayesian filters did not become popular until later, multiple programs were released in 1998 to address the growing problem of unwanted email.[19] The first scholarly publication on Bayesian spam filtering was by Sahami et al. in 1998.[20]

Variants of the basic technique have been implemented in a number of research works and commercialsoftware products.[21] Many modern mailclients implement Bayesian spam filtering. Users can also install separateemail filtering programs.Server-side email filters, such asDSPAM,Rspamd,[22]SpamAssassin,[23]SpamBayes,[24]Bogofilter, andASSP, make use of Bayesian spam filtering techniques, and the functionality is sometimes embedded withinmail server software itself.CRM114, oft cited as a Bayesian filter, is not intended to use a Bayes filter in production, but includes the ″unigram″ feature for reference.[25]

Dealing with rare words

[edit]

In the case a word has never been met during the learning phase, both the numerator and the denominator are equal to zero, both in the general formula and in the spamicity formula. The software can decide to discard such words for which there is no information available.

More generally, the words that were encountered only a few times during the learning phase cause a problem, because it would be an error to trust blindly the information they provide. A simple solution is to simply avoid taking such unreliable words into account as well.

Applying again Bayes' theorem, and assuming the classification between spam and ham of the emails containing a given word ("replica") is arandom variable withbeta distribution, some programs decide to use a corrected probability:

Pr(S|W)=sPr(S)+nPr(S|W)s+n{\displaystyle \Pr '(S|W)={\frac {s\cdot \Pr(S)+n\cdot \Pr(S|W)}{s+n}}}

where:

(Demonstration:[26])

This corrected probability is used instead of the spamicity in the combining formula.

This formula can be extended to the case wheren is equal to zero (and where the spamicity is not defined), and evaluates in this case toPr(S){\displaystyle Pr(S)}.

Other heuristics

[edit]

"Neutral" words like "the", "a", "some", or "is" (in English), or their equivalents in other languages, can be ignored. These are also known asStop words. More generally, some bayesian filtering filters simply ignore all the words which have a spamicity next to 0.5, as they contribute little to a good decision. The words taken into consideration are those whose spamicity is next to 0.0 (distinctive signs of legitimate messages), or next to 1.0 (distinctive signs of spam). A method can be for example to keep only those ten words, in the examined message, which have the greatestabsolute value |0.5 − pI|.

Some software products take into account the fact that a given word appears several times in the examined message,[27] others don't.

Some software products usepatterns (sequences of words) instead of isolated natural languages words.[28] For example, with a "context window" of four words, they compute the spamicity of "Viagra is good for", instead of computing the spamicities of "Viagra", "is", "good", and "for". This method gives more sensitivity to context and eliminates the Bayesian noise better, at the expense of a bigger database.

Disadvantages

[edit]

Depending on the implementation, Bayesian spam filtering may be susceptible toBayesian poisoning, a technique used by spammers in an attempt to degrade the effectiveness of spam filters that rely on Bayesian filtering. A spammer practicing Bayesian poisoning will send out emails with large amounts of legitimate text (gathered from legitimate news or literary sources).Spammer tactics include insertion of random innocuous words that are not normally associated with spam, thereby decreasing the email's spam score, making it more likely to slip past a Bayesian spam filter. However, with (for example)Paul Graham's scheme only the most significant probabilities are used, so that padding the text out with non-spam-related words does not affect the detection probability significantly.

Words that normally appear in large quantities in spam may also be transformed by spammers. For example, «Viagra» would be replaced with «Viaagra» or «V!agra» in the spam message. The recipient of the message can still read the changed words, but each of these words is met more rarely by the Bayesian filter, which hinders its learning process. As a general rule, this spamming technique does not work very well, because the derived words end up recognized by the filter just like the normal ones.[29]

Another technique used to try to defeat Bayesian spam filters is to replace text with pictures, either directly included or linked. The whole text of the message, or some part of it, is replaced with a picture where the same text is "drawn". The spam filter is usually unable to analyze this picture, which would contain the sensitive words like «Viagra». However, since many mail clients disable the display of linked pictures for security reasons, the spammer sending links to distant pictures might reach fewer targets. Also, a picture's size in bytes is bigger than the equivalent text's size, so the spammer needs more bandwidth to send messages directly including pictures. Some filters are more inclined to decide that a message is spam if it has mostly graphical contents. A solution used byGoogle in itsGmail email system is to perform anOCR (Optical Character Recognition) on every mid to large size image, analyzing the text inside.[30][31]

See also

[edit]

References

[edit]
  1. ^abcHand, 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.
  2. ^McCallum, Andrew."Graphical Models, Lecture2: Bayesian Network Representation"(PDF).Archived(PDF) from the original on 2022-10-09. Retrieved22 October 2019.
  3. ^abRussell, Stuart;Norvig, Peter (2003) [1995].Artificial Intelligence: A Modern Approach (2nd ed.). Prentice Hall.ISBN 978-0137903955.
  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. Retrieved24 January 2023.
  7. ^Narasimha Murty, M.; Susheela Devi, V. (2011).Pattern Recognition: An Algorithmic Approach. Springer.ISBN 978-0857294944.
  8. ^abJohn, 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. ^abcMcCallum, 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. ^James, Gareth; Witten, Daniela; Hastie, Trevor; Tibshirani, Robert (2021).An introduction to statistical learning: with applications in R (Second ed.). New York, NY: Springer. p. 157.doi:10.1007/978-1-0716-1418-1.ISBN 978-1-0716-1418-1. Retrieved10 November 2024.
  14. ^abRennie, 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.
  15. ^abNigam, 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.
  16. ^Niculescu-Mizil, Alexandru; Caruana, Rich (2005).Predicting good probabilities with supervised learning(PDF). ICML.doi:10.1145/1102351.1102430. Archived fromthe original(PDF) on 2014-03-11. Retrieved2016-04-24.
  17. ^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.
  18. ^abNg, Andrew Y.;Jordan, Michael I. (2002).On discriminative vs. generative classifiers: A comparison of logistic regression and naive Bayes.NIPS. Vol. 14.
  19. ^Brunton, Finn (2013).Spam: A Shadow History of the Internet.MIT Press. p. 136.ISBN 9780262018876.Archived from the original on 2019-03-23. Retrieved2017-09-13.
  20. ^M. Sahami; S. Dumais; D. Heckerman; E. Horvitz (1998)."A Bayesian approach to filtering junk e-mail"(PDF). AAAI'98 Workshop on Learning for Text Categorization.Archived(PDF) from the original on 2007-09-27. Retrieved2007-08-15.
  21. ^"Junk Mail Controls". MozillaZine. November 2009.Archived from the original on 2012-10-23. Retrieved2010-01-16.
  22. ^"Rspamd statistic settings". docs.rspamd.com. Retrieved2025-09-25.
  23. ^"Installation". Ubuntu manuals. 2010-09-18. Archived fromthe original on 29 September 2010. Retrieved2010-09-18.Gary Robinson's f(x) and combining algorithms, as used in SpamAssassin
  24. ^"Background Reading". SpamBayes project. 2010-09-18.Archived from the original on 6 September 2010. Retrieved2010-09-18.Sharpen your pencils, this is the mathematical background (such as it is).* The paper that started the ball rolling: Paul Graham's A Plan for Spam.* Gary Robinson has an interesting essay suggesting some improvements to Graham's original approach.* Gary Robinson's Linux Journal article discussed using the chi squared distribution.
  25. ^"Archived copy".Archived from the original on 2016-10-07. Retrieved2016-07-09.{{cite web}}: CS1 maint: archived copy as title (link)
  26. ^Gary Robinson (2003)."A statistical approach to the spam problem".Linux Journal.Archived from the original on 2010-10-22. Retrieved2007-07-19.
  27. ^Brian Burton (2003)."SpamProbe - Bayesian Spam Filtering Tweaks".Archived from the original on 2012-03-01. Retrieved2009-01-19.
  28. ^Jonathan A. Zdziarski (2004)."Bayesian Noise Reduction: Contextual Symmetry Logic Utilizing Pattern Consistency Analysis".[permanent dead link]
  29. ^Paul Graham (2002),A Plan for SpamArchived 2004-04-04 at theWayback Machine
  30. ^"Gmail uses Google's innovative technology to keep spam out of your inbox".Archived from the original on 2015-09-13. Retrieved2015-09-05.
  31. ^Zhu, Z.; Jia, Z; Xiao, H; Zhang, G; Liang, H.; Wang, P. (2014). "A Modified Minimum Risk Bayes and It's Application in Spam Filtering". In Li, S; Jin, Q; Jiang, X; Park, J (eds.).Frontier and Future Development of Information Technology in Medicine and Education. Lecture Notes in Electrical Engineering. Vol. 269. Dordrecht: Springer. pp. 2155–2159.doi:10.1007/978-94-007-7618-0_261.ISBN 978-94-007-7617-3.
  32. ^Hristea, Florentina T. (2013).The Naïve Bayes Model for Unsupervised Word Sense Disambiguation. London; Berlin: Springer- Verlag Heidelberg Berlin. p. 70.ISBN 978-3-642-33692-8.
  33. ^Zheng, J.; Tang, Yongchuan (2005). "One Generalization of the Naive Bayes to Fuzzy Sets and the Design of the Fuzzy Naive Bayes Classifier". In Mira, Jose; Álvarez, Jose R (eds.).Artificial Intelligence and Knowledge Engineering Applications: A Bioinspired Approach. Lecture Notes in Computer Science. Vol. 3562. Berlin: Springer, Berlin, Heidelberg. p. 281.doi:10.1007/11499305_29.ISBN 978-3-540-26319-7.ISSN 0302-9743.

Further reading

[edit]

External links

[edit]
Protocols
Email spam
Other
Anti-spam
Anti-spam software
Spamdexing
Internet fraud
Retrieved from "https://en.wikipedia.org/w/index.php?title=Naive_Bayes_classifier&oldid=1322860300"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp