基于最小错误率的bayes分类的MATLAB实现

更新时间:2024-01-16 18:57:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

function myBayes(N1,N2,N3) %N1,N2,N3为训练样本采样点数目 Result(1:3,1:3)=0; %判别矩阵

R1=[];r1=1; %空矩阵用来记录分类正确的点,r1记录列数 R2=[];r2=1; R3=[];r3=1;

%---------------------------------------------------% %训练样本

X1=mvnrnd([1 2],[4 0;0 6],N1)'; X2=mvnrnd([5 3],[5 0;0 1],N2)'; X3=mvnrnd([4 7],[2 0;0 9],N3)'; %测试样本

X10=mvnrnd([1 2],[4 0;0 6],100)'; X20=mvnrnd([5 3],[5 0;0 1],100)'; X30=mvnrnd([4 7],[2 0;0 9],100)';

%先验概率

P(1)=length(X1)/(length(X1)+length(X2)+length(X3)); P(2)=length(X2)/(length(X1)+length(X2)+length(X3)); P(3)=length(X3)/(length(X1)+length(X2)+length(X3)) ; %--------------------------------------------------------% Ave10=mean(X1,2);%对行求平均值 Ave20=mean(X2,2); Ave30=mean(X3,2);

%-----------------------------------------------------------% %程序核心:求出判别矩阵,并把每个测试样本点代入判别矩阵,根据结果归类 for i=1:3 for j=1:100

if i==1

d1=log(P(1))-0.5*log(det(cov(X1')))-(X10(:,j)-Ave10)'*inv(cov(X1'))*(

X10(:,j)-Ave10)*0.5;

d2=log(P(2))-0.5*log(det(cov(X2')))-(X10(:,j)-Ave20)'*inv(cov(X2'))*(

X10(:,j)-Ave20)*0.5;

d3=log(P(3))-0.5*log(det(cov(X3')))-(X10(:,j)-Ave30)'*inv(cov(X3'))*(

X10(:,j)-Ave30)*0.5; if d1>=d2&&d1>=d3 %% Result(1,1)=Result(1,1)+1; R1(:,r1)=X10(:,j); r1=r1+1;

elseif d2>=d1&&d2>=d3 %%

Result(1,2)=Result(1,2)+1;%?????ó?D?é?? else

Result(1,3)=Result(1,3)+1;%?????ó?D?é?? end elseif i==2

d1=log(P(1))-0.5*log(det(cov(X1')))-(X20(:,j)-Ave10)'*inv(cov(X1'))*

(X20(:,j)-Ave10)*0.5;

d2=log(P(2))-0.5*log(det(cov(X2')))-(X20(:,j)-Ave20)'*inv(cov(X2'))*

(X20(:,j)-Ave20)*0.5;

d3=log(P(3))-0.5*log(det(cov(X3')))-(X20(:,j)-Ave30)'*inv(cov(X3'))*

(X20(:,j)-Ave30)*0.5; if d2>=d1&&d2>=d3 %%

Result(2,2)=Result(2,2)+1; R2(:,r2)=X20(:,j); r2=r2+1;

elseif d1>=d2&&d1>=d3 %% Result(2,1)=Result(2,1)+1; else

Result(2,3)=Result(2,3)+1; end elseif i==3

d1=log(P(1))-0.5*log(det(cov(X1')))-(X30(:,j)-Ave10)'*inv(cov(X1'))*

(X30(:,j)-Ave10)*0.5;

d2=log(P(2))-0.5*log(det(cov(X2')))-(X30(:,j)-Ave20)'*inv(cov(X2'))*

(X30(:,j)-Ave20)*0.5;

d3=log(P(3))-0.5*log(det(cov(X3')))-(X30(:,j)-Ave30)'*inv(cov(X3'))*

(X30(:,j)-Ave30)*0.5;

if d3>=d1&&d3>=d2 %% Result(3,3)=Result(3,3)+1; R3(:,r3)=X30(:,j);

r3=r3+1; elseif d2>=d1&&d2>=d3 %% Result(3,2)=Result(3,2)+1; else

Result(3,1)=Result(3,1)+1; end end end end

%画出分布情况 subplot(3,1,1)

plot(X1(1,:),X1(2,:),'r.','LineWidth',2),hold on plot(X2(1,:),X2(2,:),'go','LineWidth',2),hold on plot(X3(1,:),X3(2,:),'b+','LineWidth',2),hold on title('训练样本分布情况')

legend('训练样本1','训练样本2','训练样本3') subplot(3,1,2)

plot(X10(1,:),X10(2,:),'r.','LineWidth',2),hold on plot(X20(1,:),X20(2,:),'go','LineWidth',2),hold on

plot(X30(1,:),X30(2,:),'b+','LineWidth',2),hold on title('测试样本分布情况')

legend('测试样本1','测试样本2','测试样本3') subplot(3,1,3)

plot(R1(1,:),R1(2,:),'r.','LineWidth',2),hold on plot(R2(1,:),R2(2,:),'go','LineWidth',2),hold on plot(R3(1,:),R3(2,:),'b+','LineWidth',2),hold on title('被正确分类点的的分布情况') legend('分类1','分类2','分类3')

Result %输出判别矩阵 Err23=Result(1,1)/100; Err13=Result(2,2)/100; Err12=Result(3,3)/100; disp('第一类分类正确率: '); Err23

disp('第二类分类正确率: '); Err13

disp('第三类分类正确率: '); Err12

本文来源:https://www.bwwdw.com/article/z1fo.html

Top