贝叶斯分类原理:


1)在已知P(Wi),P(X|Wi)(i=1,2)及给出待识别的X的情况下,根据贝叶斯公式计算出后验概率P(Wi|X) ;

2)根据1)中计算的后验概率值,找到最大的后验概率,则样本X属于该类。

举例:


解决方案:

 

但对于两类来说,因为分母相同,所以可采取如下分类标准:

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%By Shelley from NCUT,April 14th 2011

%Email:just_for_h264@163.com

%此程序利用贝叶斯分类算法,首先对两类样本进行训练,

%进而可在屏幕上任意取点,程序可输出属于第一类,还是第二类

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;

close all

 

%读入两类训练样本数据

load data

%求两类训练样本的均值和方差

u1=mean(Sample1);

u2=mean(Sample2);

sigm1=cov(Sample1); 

sigm2=cov(Sample2);

%计算两个样本的密度函数并显示

x=-20:0.5:40;

y= -20:0.5:20;

[X,Y] = meshgrid(x,y);

F1 = mvnpdf([X(:),Y(:)],u1,sigm1);

F2 = mvnpdf([X(:),Y(:)],u2,sigm2);

P1=reshape(F1,size(X));

P2=reshape(F2,size(X));

figure(2)

surf(X,Y,P1)

hold on

surf(X,Y,P2)

shading interp

colorbar

title('条件概率密度函数曲线');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%以下为测试部分

%利用ginput随机选取屏幕上的点(可连续取10个点)

%程序可根据点的位置自动地显示出属于那个类

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pw1=0.4;pw2=0.6;

 

figure(1)

plot(Sample1(:,1),Sample1(:,2),'r.')

hold on

plot(Sample2(:,1),Sample2(:,2),'b.')

 

for i=1:10

    [u,v]=ginput(1);

    plot(u,v,'m*');

    P1=pw1*mvnpdf([u,v],u1,sigm1);

    P2=pw2*mvnpdf([u,v],u2,sigm2);

    hold all

    if(P1>P2)

         disp('it belong to the first class');

    else

         disp('it belong to the second class');

    end;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

结果示意:

两个样本的密度函数曲线:



 

测试结果:


 

命令窗口中显示:

it belong to the first class

it belong to the second class

it belong to the second class

it belong to the first class

it belong to the first class

it belong to the first class

it belong to the first class

it belong to the first class

it belong to the first class

it belong to the first class

分析可知在第一类周围有八个随机的测试点,在第二类周围有两个随机的测试点,与命令窗口中的结果相符合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值