Professional Documents
Culture Documents
PRSL 9
PRSL 9
PRSL 9
• Classifier that uses the Bayes formula, that describes the conditional
probability of an event given another event:
A, B – events
P(A|B) – probability of a A given that B is true
P(B|A) – probability of a B given that A is true
P(A), P(B) – independent probabilities of A and B
Naive Bayesian Classifier
• Given a new sample that does not belong to S (it belongs to the test
set). Given the features x of the new sample, to which class does this
sample belong?
• We make the “naive” assumption that the features are independent one from
the other in order to simplify computation
Naive Bayesian Classifier
• Given a new sample and its features x, to which class does this
sample belong?
• For each class ci from C:
• Compute Pi be the probability of the new sample to belong to the class ci
• Pi = P(c = ci|x) = P(ci) * P(x|c=ci) / P(x)
For each feature xj from the feature vector x of the new sample:
• Pj = the fraction of samples with class ci from S which have the feature from j equal
to xj
P(x|c=ci) *= Pj (compute the product)
• The likelihood of having feature j equal to 255 given class i is given by the fraction of the
training instances which have feature j equal to 255 and are from class i:
• To avoid precision problems, it is recommended to work with the logarithm (product of 784 values less
than 1)
• Which is equivalent to:
• Mat prob(C,1,CV_64FC1); - probability to belong to each class
• for c = 0, C:
• prob[c, 0] = log(priors[c, 0])
• for j = 0, d:
prob[c, 0] += log(likelihood[c, j]) if test_feat[0, j] == 255 else log(1 - likelihood[c, j])
• The sample will be classified into class c for which prob[c] is max
}
Practical work
1. Load each image from the training set, perform binarization and save
the values in the training matrix X. Save the class label in the label
vector y. For the initial version use only the first 100 images from the
first two classes.
2. Implement the training method.
2.1. Compute and save the priors for each class.
2.2. Compute and save the likelihood values for each class and
each feature. Apply Laplace smoothing to avoid zero values.
3. Implement the Naive Bayes classifier for an unknown image