2017-06-04 106 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); 

我必須寫

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

所以精度是相同的功能在MATLAB構建。