Naive Bayes Classifier (NBC)
The underlying concept of naive Bayes classifier (NBC) is straightforward. If we assume that the features of the given dataset are independent, then we can perform PDF modeling on each dimension sequentially. The final PDF (probability density function) for each class is then given by the multiplication of the PDF of the same class over each dimension. In practice, we can apply 1D Gaussian functions for modeling the data of a specific class and a specific dimension.
Contents
A basic example
In the following example, we shall use dimensions 3 and 4 of the Iris dataset for NBC:
[DS, TS]=prData('iris'); DS.input=DS.input(3:4, :); % Only use the last 2 dim TS.input=TS.input(3:4, :); % Only use the last 2 dim nbcPrm=nbcTrain(DS); cOutputDs=nbcEval(DS, nbcPrm); rrDs=sum(DS.output==cOutputDs)/length(DS.output); fprintf('Inside-test recog. rate = %g%%\n', rrDs*100); cOutputTs=nbcEval(TS, nbcPrm); rrTs=sum(TS.output==cOutputTs)/length(TS.output); fprintf('Outside-test recog. rate = %g%%\n', rrTs*100);
Inside-test recog. rate = 94.6667% Outside-test recog. rate = 97.3333%
In the above example, we can obtain an inside-test recognition rate of 96%. Note that implicitly, we use the number of entries of each class as the class prior probability for computing the probability density. In general, there are two ways to specify class prior probabilities:
- Use the number of entries in each class as the prior probabilities. You can divide the total number of entries in the training set to have a real probability between 0 and 1. In practice, the division does not change the classification results since it is applied to every class' PDF. (You can use the function classDataCount.m to find the number of entries in each class.)
- If we have the domain knowledge of the prior probabilities, we can assign them directly.
1-D PDFs
We can plot the 1d PDFs for all classes and dimensions, together with the corresponding data points, as follows:
nbcPlot(DS, nbcPrm, '1dPdf');
2-D PDFs and posterior probabilities
For the above example, we can go one step further to plot the class-specific PDFs as 3D surfaces and display the corresponding contours, as follows:
nbcPlot(DS, nbcPrm, '2dPdf');
The 2D posterior probability function for each class can be display similarly:
nbcPlot(DS, nbcPrm, '2dPosterior');
Decision boundary
Based on the computed PDF for each class, we can plot the decision boundaries along with the design set, as follows:
DS.hitIndex=find(cOutputDs==DS.output); % This is used in nbcPlot. nbcPlot(DS, nbcPrm, 'decBoundary');
The decision boundaries and the test set can be plotted similarly:
TS.hitIndex=find(cOutputTs==TS.output); % This is used in nbcPlot. nbcPlot(TS, nbcPrm, 'decBoundary');
Copyright 2011-2016 Jyh-Shing Roger Jang.