Bayesian Classifier Notes
Bayesian Classifier Notes
Bayesian Classifier Notes
The Bayesian classifier is an algorithm for classifying multiclass datasets. This is based on the
Bayes’ theorem in probability theory. Bayes in whose name the theorem is known was an English
statistician who was known for having formulated a specific case of a theorem that bears his name.
The classifier is also known as “naive Bayes Algorithm” where the word “naive” is an English word
with the following meanings: simple, unsophisticated, or primitive. We first explain Bayes’ theorem
and then describe the algorithm. Of course, we require the notion of conditional probability.
61
CHAPTER 6. BAYESIAN CLASSIFIER AND ML ESTIMATION 62
Remarks
Consider events and respective probabilities as shown in Figure 6.1. It can be seen that, in this case,
the conditions Eqs.(6.1)–(6.3) are satisfied, but Eq.(6.4) is not satisfied. But if the probabilities are
as in Figure 6.2, then Eq.(6.4) is satisfied but all the conditions in Eqs.(6.1)–(6.2) are not satisfied.
Figure 6.1: Events A, B, C which are not mutually independent: Eqs.(6.1)–(6.3) are satisfied, but
Eq.(6.4) is not satisfied.
Figure 6.2: Events A, B, C which are not mutually independent: Eq.(6.4) is satisfied but Eqs.(6.1)–
(6.2) are not satisfied.
P (A∣B)P (B)
P (B∣A) = .
P (A)
6.2.2 Remarks
1. The importance of the result is that it helps us to “invert” conditional probabilities, that is, to
express the conditional probability P (A∣B) in terms of the conditional probability P (B∣A).
6.2.3 Generalisation
Let the sample space be divided into disjoint events B1 , B2 , . . . , Bn and A be any event. Then we
have
P (A∣Bk )P (Bk )
P (Bk ∣A) = n
∑i=1 P (A∣Bi )P (Bi )
6.2.4 Examples
Problem 1
Consider a set of patients coming for treatment in a certain clinic. Let A denote the event that
a “Patient has liver disease” and B the event that a “Patient is an alcoholic.” It is known from
experience that 10% of the patients entering the clinic have liver disease and 5% of the patients are
alcoholics. Also, among those patients diagnosed with liver disease, 7% are alcoholics. Given that
a patient is alcoholic, what is the probability that he will have liver disease?
Solution
Using the notations of probability, we have
Problem 2
Three factories A, B, C of an electric bulb manufacturing company produce respectively 35%. 35%
and 30% of the total output. Approximately 1.5%, 1% and 2% of the bulbs produced by these
factories are known to be defective. If a randomly selected bulb manufactured by the company was
found to be defective, what is the probability that the bulb was manufactures in factory A?
Solution
Let A, B, C denote the events that a randomly selected bulb was manufactured in factory A, B, C
respectively. Let D denote the event that a bulb is defective. We have the following data:
We are required to find P (A∣D). By the generalisation of the Bayes’ theorem we have:
P (D∣A)P (A)
P (A∣D) =
P (D∣A)P (A) + P (D∣B)P (B) + P (D∣C)P (C)
0.015 × 0.35
=
0.015 × 0.35 + 0.010 × 0.35 + 0.020 × 0.30
= 0.356.
CHAPTER 6. BAYESIAN CLASSIFIER AND ML ESTIMATION 64
X = (x1 , x2 , . . . , xn ).
We are required to determine the most appropriate class label that should be assigned to the test
instance. For this purpose we compute the following conditional probabilities
and choose the maximum among them. Let the maximum probability be P (ci ∣X). Then, we choose
ci as the most appropriate class label for the training instance having X as the feature vector.
The direct computation of the probabilities given in Eq.(6.5) are difficult for a number of reasons.
The Bayes’ theorem can b applied to obtain a simpler method. This is explained below.
P (ck ∣X) ∝ P (x1 ∣ck )P (x2 ∣ck )⋯P (xn ∣ck )P (ck ).
Remarks
The various probabilities in the above expression are computed as follows:
No. of examples with class label ck
P (ck ) =
Total number of examples
No. of examples with jth feature equal to xj and class label ck
P (xj ∣ck ) =
No. of examples with class label ck
Let there be a training data set having n features F1 , . . . , Fn . Let f1 denote an arbitrary value of F1 ,
f2 of F2 , and so on. Let the set of class labels be {c1 , c2 , . . . , cp }. Let there be given a test instance
having the feature vector
X = (x1 , x2 , . . . , xn ).
We are required to determine the most appropriate class label that should be assigned to the test
instance.
Step 1. Compute the probabilities P (ck ) for k = 1, . . . , p.
Step 2. Form a table showing the conditional probabilities
for k = 1, . . . , p.
Step 4. Find j such qj = max{q1 , q2 , . . . , qp }.
Step 5. Assign the class label cj to the test instance X.
Remarks
In the above algorithm, Steps 1 and 2 constitute the learning phase of the algorithm. The remaining
steps constitute the testing phase. For testing purposes, only the table of probabilities is required;
the original data set is not required.
6.3.5 Example
Problem
Consider a training data set consisting of the fauna of the world. Each unit has three features named
“Swim”, “Fly” and “Crawl”. Let the possible values of these features be as follows:
Swim Fast, Slow, No
Fly Long, Short, Rarely, No
Crawl Yes, No
For simplicity, each unit is classified as “Animal”, “Bird” or “Fish”. Let the training data set be as in
Table 6.1. Use naive Bayes algorithm to classify a particular species if its features are (Slow, Rarely,
No)?
CHAPTER 6. BAYESIAN CLASSIFIER AND ML ESTIMATION 66
Solution
In this example, the features are
F1 = “Swim”, F2 = “Fly”, F3 = “Crawl”.
The class labels are
c1 = “Animal”, c2 = “ Bird”, c3 = “Fish”.
The test instance is (Slow, Rarely, No) and so we have:
x1 = “Slow”, x2 = “Rarely”, x3 = “No”.
We construct the frequency table shown in Table 6.2 which summarises the data. (It may be noted
that the construction of the frequency table is not part of the algorithm.)
Features
Class Swim (F1 ) Fly (F2 ) Crawl (F3 ) Total
Fast Slow No Long Short Rarely No Yes No
Animal (c1 ) 2 2 1 0 0 1 4 2 3 5
Bird (c2 ) 1 0 3 1 2 0 1 1 3 4
Fish (c3 ) 1 2 0 0 0 0 3 0 3 3
Total 4 4 4 1 2 1 8 4 8 12
Features
Swim (F1 ) Fly (F2 ) Crawl (F3 )
Class
f1 f2 f3
Fast Slow No Long Short Rarely No Yes No
Animal (c1 ) 2/5 2/5 1/5 0/5 0/5 1/5 4/5 2/5 3/5
Bird (c2 ) 1/4 0/4 3/4 1/4 2/4 0/4 1/4 0/4 4/4
Fish (c3 ) 13 2/3 0/3 0/3 0/3 0/3 3/3 0/3 3/3
Step 4. Now
max{q1 , q2 , q3 } = 0.05.
c1 = “ Animal”.
So we assign the class label “Animal” to the test instance “(Slow, Rarely, No)”.
2. If there are no obvious cut points, we may discretize the feature using quantiles. We may
divide the data into three bins with tertiles, four bins with quartiles, or five bins with quintiles,
etc.
Definition
Maximum likelihood estimation (MLE) is a method of estimating the parameters of a statistical
model, given observations. MLE attempts to find the parameter values that maximize the likelihood
function, given the observations. The resulting estimate is called a maximum likelihood estimate,
which is also abbreviated as MLE.
In maximum likelihood estimation, we find the value of θ that makes the value of the likelihood
function maximum. For computation convenience, we define the log likelihood function as the
logarithm of the likelihood function:
A value of θ that maximizes L(θ) will also maximise l(θ) and vice-versa. Hence, in maximum like-
lihood estimation, we find θ that maximizes the log likelihood function. Sometimes the maximum
likelihood estimate of θ is denoted by θ̂.
Estimation of p
Consider a random sample X = {x1 , . . . , xn } taken from a Bernoulli distribution with the probability
function f (x∣p). The log likelihood function is
L(p) = log f (x1 ∣p) + ⋯ + log f (xn ∣p)
= log px1 (1 − p)1−x1 + ⋯ + log pxn (1 − p)1−xn
= [x1 log p + (1 − x1 ) log(1 − p)] + ⋯ + [xn log p + (1 − xn ) log(1 − p)]
To find the value of p that maximizes L(p) we set up the equation
dL
= 0,
dp
that is,
x1 1 − x1 xn 1 − xn
[ − ]+⋯+[ − ] = 0.
p 1−p p 1−p
Solving this equation, we have the maximum likelihood estimate of p as
1
p̂ = (x1 + ⋯ + xn ).
n
2. Multinomial density
Suppose that the outcome of a random event is one of K classes, each of which has a probability of
occurring pi with
p1 + ⋯ + pK = 1.
We represent each outcome by an ordered K-tuple x = (x1 , . . . , xK ) where exactly one of x1 , . . . , xK
is 1 and all others are 0. xi = 1 if the outcome in the i-th class occurs. The probability function can
be expressed as
f (x∣p, . . . , pK ) = px1 1 . . . pxKK .
Here, p1 , . . . , pK are the parameters.
We choose n random samples. The i-the sample may be represented by
xi = (x1i , . . . , xKi ).
The values of the parameters that maximizes the likelihood function can be shown to be
1
p̂k = (xk1 + xk2 + ⋯ + xkn ).
n
(We leave the details of the derivation as an exercise.)