实现朴素贝叶斯分类器的低精度

时间:2017-06-04 15:42:55

标签: matlab floating-accuracy naivebayes

我有朴素贝叶斯分类器的代码,它实现了朴素贝叶斯的概念,但是这个算法给我的准确度大约是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

1 个答案:

答案 0 :(得分:0)

我只是解决它。而不是这一行

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

我必须写

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

因此准确性与matlab在功能中的构建相同。