• 大小: 4KB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2024-01-27
  • 语言: Matlab
  • 标签: bayes  

资源简介

使用Matlab实现,包括一维特征最小错误率bayes分类器;二维特征最小错误率bayes分类器;二维特征最小风险bayes分类器以及使用的数据集合。

资源截图

代码片段和文件信息

%%%Bayes Classifier
%%%贝叶斯分类器根据二维特征(身高和体重)
%%%date:2011-6-5
clear all

%训练数据
[mstature(:1)mstature(:2)]=textread(‘male.txt‘‘%f%f‘‘headerlines‘0);
[fstature(:1)fstature(:2)]=textread(‘female.txt‘‘%f%f‘‘headerlines‘0);
[row1col1]=size(mstature);
[row2col2]=size(fstature);

%测试数据
[test1(:1)test1(:2)test1(:3)]=textread(‘test1.txt‘‘%f%f%d‘‘headerlines‘0);
[test2(:1)test2(:2)test2(:3)]=textread(‘test2.txt‘‘%f%f%d‘‘headerlines‘0);
[test1_row test1_col]=size(test1);
[test2_row test2_col]=size(test2);

%画出测试样本数据的二维直角坐标图
figure(1)
for i=1:test1_row
    if(test1(i3)==1)
        plot(test1(i1)test1(i2)‘bx‘);
    elseif(test1(i3)==-1)
        plot(test1(i1)test1(i2)‘g+‘);
    end
    hold on;
end

for i=1:test2_row
    if(test2(i3)==1)
        plot(test2(i1)test2(i2)‘bx‘);
    elseif(test2(i3)==-1)
        plot(test2(i1)test2(i2)‘g+‘);
    end
    hold on;
end

%%最大似然法(正态分布假设)(以身高和体重为特征)
%male
m1=mean(mstature);
d1=cov(mstature);

%female
m2=mean(fstature);
d2=cov(fstature);

%初始化风险函数
r11=0;
r12=1;
r21=1;
r22=0;

%对测试样本test1和test2分类
right=0;%分类正确次数
wrong=0;%分类错误次数
%初始化先验概率
pw1=0.5;  %0.5  0.75    0.9
pw2=0.5;  %0.5  0.25    0.1
LR=((r12-r22)/(r21-r11))*(pw2/pw1)

%测试数据集1
fprintf(‘******************************************\n‘);
fprintf(‘测试数据集test1\n‘);
fprintf(‘******************************************\n‘);
for i=1:test1_row
    px1=mvnpdf([test1(i1)test1(i2)]m1d1);
    px2=mvnpdf([test1(i1)test1(i2)]m2d2);
    
    
    if (px1/px2)>=LR
        fprintf(‘测试数据(%f%f)属于w1类(男)‘test1(i1)test1(i2));
        plot(test1(i1)test1(i2)‘rs‘); 
        if(test1(i3)==1)
            right=right+1;
            fprintf(‘  正确\n‘);
        else
            wrong=wrong+1;
            fprintf(‘  错误\n‘);
        end
    else
        fprintf(‘测试数据(%f%f)属于w2类(女)‘test1(i1)test1(i2));
        plot(test1(i1)test1(i2)‘ko‘); 
        if(test1(i3)==-1)
            right=right+1;
            fprintf(‘  正确\n‘);
        else
            wrong=wrong+1;
            fprintf(‘  错误\n‘);
        end
    end
end
        
%测试数据集2
fprintf(‘******************************************\n‘);
fprintf(‘测试数据集test2\n‘);
fprintf(‘******************************************\n‘);
for i=1:test2_row
    px1=mvnpdf([test2(i1)test2(i2)]m1d1);
    px2=mvnpdf([test2(i1)test2(i2)]m2d2);    
        
    if (px1/px2)>=LR
        fprintf(‘测试数据(%f%f)属于w1类(男)‘test2(i1)test2(i2));
        plot(test2(i1)test2(i2)‘rs‘); 
        if(test2(i3)==1)
            right=right+1;
            fprintf(‘  正确\n‘);
        else
            wrong=wrong+1;
            fprintf(‘  错误\n‘);
        end
    else
        fprintf(‘测试数据(%f%f)属于w2类(女)‘test2(i1)test2(i2));
        plot(test2(i1)test2(i2)‘ko‘); 
        if(test2(i3)==-1)
            right=right+1;
            fprintf(‘  正确\n‘);
        else
            wrong=wro

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3386  2011-10-14 00:11  Bayes\bayes.m

     文件       3450  2011-10-14 00:12  Bayes\bayes1D.m

     文件       3440  2011-10-14 00:12  Bayes\bayes2D.m

     文件        426  2004-10-08 09:03  Bayes\FEMALE.TXT

     文件        424  2004-10-08 09:04  Bayes\MALE.TXT

     文件        350  2004-10-08 09:03  Bayes\test1.txt

     文件       3020  2004-10-08 09:10  Bayes\test2.txt

     目录          0  2011-10-14 00:17  Bayes

----------- ---------  ---------- -----  ----

                14496                    8


评论

共有 条评论