-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartialBayesClassifier.m
More file actions
61 lines (54 loc) · 2.12 KB
/
Copy pathpartialBayesClassifier.m
File metadata and controls
61 lines (54 loc) · 2.12 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [out] = partialBayesClassifier(classes,selection,teste,trndat,words,binary)
% Nota: não foi usada esta função (ainda) para a aplicação do NaiveBayes no
% projeto, para ver implementação atual ver código do main.m ou testeNB.m
%partialBayesClassifier - The Bayes classifier is the classifier having the
% smallest probability of misclassification of all classifiers using the
% same set of features. (Source: Wikipedia)
%
% This function in particular does *NOT* perform comparisons between
% classifiers (hence 'partial'), therefore requiring external comparisons
% for the sake of simplicity (for now...).
%
% Syntax
% P = classiBayes(classes,selection,teste,mat,words)
% P = classiBayes(classes,selection,teste,mat,words,1)
%
% Input Arguments
% classes - classes assigned for training data
% cell row of character vectors
% selection - selected class to calculate the classifier c_selection
% scalar string
% teste - given occurrence row
% numeric array | string
% trndat - training data
% matrix
% words - (unique) words avaiable to process from training data
% cell row of character vectors
% binary - whether we're handling binary Naïve Bayes or not
% 0 -> false | other number -> true
if nargin == 5
binary = 0;
end
psel = sum(strcmp(classes,selection))/length(classes);
rowsfromsel = trndat(strcmp(classes,selection),:); % rows that possess the wanted class
gdtotal = sum(rowsfromsel(:));
probabilities = [];
%for wrd=split(teste)' used in case if they were strings, which was now updated
for iwrd=1:length(teste)
clgw = rowsfromsel(:,iwrd); % column with word occurrence for each row
if binary == 0
if isempty(clgw +1)
continue
end
pred = sum(clgw)+1; % we sum it all to get n_k
probabilities(end+1) = ((pred)/(gdtotal+length(words)))^(teste(iwrd)); % P(w_k|c_j)
else
if teste(iwrd) == 0
continue
end
pred = sum(clgw); % we sum it all to get n_k
probabilities(end+1) = (pred)/(gdtotal); % P(w_k|c_j)
end
end
out = prod(probabilities)*psel;
end