-
Notifications
You must be signed in to change notification settings - Fork 3
/
naivebayes.m
executable file
·26 lines (23 loc) · 953 Bytes
/
naivebayes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
classdef naivebayes < classifier
% NAIVEBAYES extend classifier superclass by providing method to calculate
% the probabilities for the whole document Pr(document|category) by
% combining the probabilities of features in the document.
% Because of inheritance, 'naivebayes' will have the same properties and
% methods with superclass 'classifier'. So we only have to define unique
% add-on functionalities in 'naivesbayes'.
methods
function p=docprob(self,item,cat)
features=self.getfeatures(item);
% Multiply the probabilities of all the features together
p=1;
for i=1:size(features,1)
p=p*self.weightedprob(features{i},cat,@self.fprob);
end
end
function p=prob(self,item,cat)
catprob=self.catcount(cat)/self.totalcount();
docprob=self.docprob(item,cat);
p=docprob*catprob;
end
end
end