2017-06-04 35 views
-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 
+0

プログラムとMatlabの間で全く同じトレーニングデータセットを使用していますか? – Zimano

+0

@Zimanoはいそうです。私は関数と私のビルドのモデルパラメータをチェックし、彼らは同じです。私は予測段階でいくつか問題があると思う。しかし、私はどこを知りません – Elnaz

答えて

0

私はそれを解決します。代わりに、このライン

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

の私はとても精度が機能でMatlabのビルドと同じである

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

を記述する必要があります。

関連する問題