【发布时间】:2017-11-05 10:51:20
【问题描述】:
我有实现朴素贝叶斯概念的朴素贝叶斯分类器代码,但该算法给我的准确率约为 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
【问题讨论】:
-
您的程序和 Matlab 之间是否使用完全相同的训练数据集?
-
@Zimano 是的,我愿意。我检查了内置函数和我的模型参数,它们是相同的。我认为我在预测阶段有一些问题。但我不知道在哪里
标签: matlab floating-accuracy naivebayes