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

Matlab 朴素贝叶斯分类器实现精度低,matlab,floating-accuracy,naivebayes,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); %

我有实现朴素贝叶斯概念的朴素贝叶斯分类器的代码,但该算法给出的准确率约为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

我只是解决它。而不是这条线

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

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

因此,精度与matlab内置函数相同。

您的程序和matlab之间是否使用完全相同的训练数据集?@Zimano是的,我使用。我检查了内置函数和我的模型参数,它们是相同的。我想我在预测阶段遇到了一些问题。但是我不知道在哪里