提问人:Elnaz 提问时间:6/4/2017 更新时间:6/6/2017 访问量:357
实现朴素贝叶斯分类器的准确率低
Low Accuracy in Implementing naive Bayes classifier
问:
我有朴素贝叶斯分类器的代码,实现了朴素贝叶斯的概念,但该算法给我的准确率约为 48%,远低于朴素贝叶斯的 MATLAB 内置函数 (84%)。谁能帮我问题出在哪里? 这是我的代码:
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
答:
0赞
Elnaz
6/6/2017
#1
我只是解决它。而不是这一行
fu = normcdf(ones(nc,1)*u(j,:), mu, sigma);
我必须写
fu = normpdf(ones(nc,1)*u(j,:), mu, sigma);
因此,精度与MATLAB内置函数相同。
评论