⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 naivebayes.m

📁 尽管matlab提供了朴素bayes的函数
💻 M
字号:
%------------------------------------------------------------
% NaiveBayesian Classification
%-----------------------------------------------------------
% Parameter:
%  k: Number of Cluster;

clear all
close all


k = 3;
attribute = 4;

%------------------------------------------------------
load iris_tr;
Y= IRIS_training_data;
clear IRIS_training_data
%---------------------------------------------------------
% Learning process
%------------------------------ Calculating  P(Ci)
[nr,nc] = size(Y);
for i= 1:1:k
    pc(i) = sum(Y(:, attribute+i))/nr;
end
%--------------------------------------------------------------------
% Calculating Mean and Standard Error for each Class and Attribute 
%----------------------------------------------------------------------
for i = 1:1:k
    t = 1;
    for j = 1:1:nr
        if Y(j, attribute+i) == 1
            x(t,:) = Y(j, 1:attribute);
            t = t+1;
        end
    end
    mu(i,:)  = mean(x);
    xigma(i,:) = std(x);
    clear x
end
%-----------------------------------------------------------------
%----------------------------------------------------------------
% Testing process
%----------------------------------------------------------------
% Load test data set
%--------------------------------------------
load iris_te;
X= IRIS_testing_data;
clear IRIS_testing_data

% load iris_tr;
% X= IRIS_training_data;
% clear IRIS_training_data
%----------------------------------------------
[xr,xc] = size(X);

Correct = 0;
for i = 1:1:xr
%--------------------------------------------------------------
% Calculating P(xi | Ci) for given data X(i,:)
%-------------------------------------------------------------
    for p = 1:1:k
        for j = 1:1:attribute
            temp1 = -(X(i,j) - mu(p, j))^2 / (2 * xigma(p,j)^2);
            temp2 = 1/ (sqrt(2*pi) * xigma(p, j));
            pxc(p,j) = temp2 * exp(temp1);
        end
    end
%--------------------------------------------------------------
% Calculating P(X| Ci)*P(Ci) for given data X(i,:); 
%  and assign  X(i,:) into a Class.
%-------------------------------------------------------------   
    pxcc = ones(1,k);
    p_min = -1;
    for p = 1:1:k
        for j = 1:1:attribute
            pxcc(p) = pxcc(p) * pxc(p,j);
        end
        pxcc(p) = pxcc(p) * pc(p);
        
        if pxcc(p) > p_min
            p_min = pxcc(p);
            class = p;
        end
    end
    
    class_label(i) = class;
%--------------------------------------------------------------
% Calculating Calssification Precision 
%-------------------------------------------------------------    
    if X(i,attribute+class) == 1
        Correct = Correct + 1;
    end
end
    
Precision = Correct/xr
Err = xr - Correct

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -