文档库 最新最全的文档下载
当前位置:文档库 › 贝叶斯分类器的matlab实现

贝叶斯分类器的matlab实现

贝叶斯分类器的matlab实现
贝叶斯分类器的matlab实现

贝叶斯分类器的matlab实现

贝叶斯分类原理:

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@https://www.wendangku.net/doc/8814309193.html,

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

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

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

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

相关文档