-1

I have code for naive Bayes classifier that implement the concept of the naive Bayes, but the accuracy that this algorithm gives me is about 48% and it much lower than MATLAB build-in function for Naive Bayes (84%). Can anybody help me where is the problem? here is my code:

    function [conf, confMat] =  NaiveBayesClassifier(train, test)

Att_cnt = size(train, 2) - 1;

% training set
x = train(:, 1:Att_cnt);
y = train(:, Att_cnt+1);
% test set
u = test(:, 1:Att_cnt);
v = test(:, Att_cnt+1);

yu = unique(y);
nc = length(yu); % number of classes
ni = size(x,2); % independent variables
ns = length(v); % test set

% compute class probability
for i = 1 : nc
    fy(i) = sum(double(y==yu(i)))/length(y);
end


% normal distribution
% parameters from training set
[mu, sigma] = MLE(train);

% probability for test set
for j = 1 : ns
    fu = normcdf(ones(nc,1)*u(j,:), mu, sigma);
    P(j,:)= fy.*prod(fu,2)';
end

% get predicted output for test set
[pv0, id] = max(P,[],2);
for i = 1 : length(id)
    pv(i,1) = yu(id(i));
end

% compare predicted output with actual output from test data
confMat = confusionmat(v,pv);
conf = sum(pv==v)/length(pv);

end
Elnaz
  • 3
  • 1
  • Are you using the exact same training dataset between your program and Matlab? – Zimano Jun 04 '17 at 16:14
  • @Zimano yes i do. I check the model parameters for build in function and mine, they are the same. I think i have some problem in prediction phase. but i dont know where – Elnaz Jun 04 '17 at 16:45

1 Answers1

0

I just solve it. instead of this line

fu = normcdf(ones(nc,1)*u(j,:), mu, sigma);

i have to write

fu = normpdf(ones(nc,1)*u(j,:), mu, sigma);

so the accuracy is the same as matlab build in function.

Elnaz
  • 3
  • 1