【问题标题】:Low Accuracy in Implementing naive Bayes classifier实现朴素贝叶斯分类器的准确性低
【发布时间】: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


【解决方案1】:

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

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

我要写

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

所以精度和matlab内置函数一样。

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 2012-07-31
    • 2016-08-12
    • 2017-12-21
    • 2012-01-30
    • 2017-11-06
    • 2018-01-13
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多