资源简介

用MatLab编写的正态分布模式下的贝叶斯分类器。很多模式识别的课设就有相关题目。不仅有样本分类而且会在二维坐标系下画出,正确分类与错误分类的点都会有不同的标志。

资源截图

代码片段和文件信息

clear; clc;
N = 30
C1 =[3.50;03.5];
C2 = [3.50;03.5];
M1 = [-22];
M2 = [2-2];
w1=mvnrnd(M1C130);   %产生随即数
w2 =mvnrnd(M2C230);
f1 = 0;
f2 = 0;
M1 = [00];
M2 = [00];
C1 = [00;00];
C2 = [00;00];
mm = max(abs(w1));       %控制样本特征值在[-55]之间
mm = max(mm);
w1 = w1/mm*5
mm = max(abs(w2));
mm = max(mm);
w2 = w2/mm*5
for i = 1:N
    M1 = M1 + w1(i:);
    M2 = M2 + w2(i:);
end
M1 = M1/N
M2 = M2/N
for i=1:N
    C1 = C1 + transpose(w1(i:))*w1(i:)-transpose(M1)*M1;
    C2 = C2 + transpose(w2(i:))*w2(i:)-transpose(M2)*M2;
end
C1 = C1/N
C2 = C2/N


figure(1);                                                                      %以样本的第一个特征值进行分类
axis([-55-55]);
hold on
grid on
f1=0;
f2=0;
for i=1:N
   t=(w1(i1)-M1(1))*(1/C1(11))*(w1(i1)-M1(1));
   d1 = log(0.5)-0.5*log(C1(11))-0.5*t;
   t=(w1(i1)-M2(1))*(1/C2(11))*(w1(i1)-M2(1));
   d2 = log(0.5)-0.5*log(C2(11))-0.5*t;
   if d1       plot(w1(i1)w1(i2)‘rx‘)
       f1 = f1+1;
   else
       plot(w1(i1)w1(i2)‘ro‘)
   end
   t=(w2(i1)-M1(1))*(1/C1(11))*(w2(i1)-M1(1));
   d1 = log(0.5)-0.5*log(C1(11))-0.5*t;
   t=(w2(i1)-M2(1))*(1/C2(11))*(w2(i1)-M2(1));
   d2 = log(0.5)-0.5*log(C2(11))-0.5*t;
   if d2       plot(w2(i1)w2(i2)‘x‘)
       f2 = f2+1;
   else
       plot(w2(i1)w2(i2)‘o‘)
   end
end
p1 = f1/N*100
p2 = f2/N*100

figure(2);                                                                              %以样本的第二个特征值进行分类
axis([-55-55]);
hold on
grid on
f1=0;
f2=0;
for i=1:N
   t=(w1(i2)-M1(2))*(1/C1(22))*(w1(i2)-M1(2));
   d1 = log(0.5)-0.5*log(C1(22))-0.5*t;
   t=(w1(i2)-M2(2))*(1/C2(22))*(w1(i2)-M2(2));
   d2 = log(0.5)-0.5*log(C2(22))-0.5*t;
   if d1       plot(w1(i1)w1(i2)‘rx‘)
       f1 = f1+1;
   else
       plot(w1(i1)w1(i2)‘ro‘)
   end
   t=(w2(i2)-M1(2))*(1/C1(22))*(w2(i2)-M1(2));
   d1 = log(0.5)-0.5*log(C1(11))-0.5*t;
   t=(w2(i2)-M2(2))*(1/C2(22))*(w2(i2)-M2(2));
   d2 = log(0.5)-0.5*log(C2(22))-0.5*t;
   if d2       plot(w2(i1)w2(i2)‘x‘)
       f2 = f2+1;
   else
       plot(w2(i1)w2(i2)‘o‘)
   end
end
p1 = f1/N*100
p2 = f2/N*100


figure(3)                                                                           %以样本的两个特征值进行分类
axis([-55-55]);
f1 = 0;
f2 = 0;
hold on
grid on
for i=1:N
   t=(w1(i:)-M1)*inv(C1)*transpose(w1(i:)-M1);
   d1 = log(0.5)-0.5*log(det(C1))-0.5*t;
   t=(w1(i:)-M2)*inv(C2)*transpose(w1(i:)-M2);
   d2= log(0.5)-0.5*log(det(C2))-0.5*t;
   if d1       plot(w1(i1)w1(i2)‘rx‘)
       f1 = f1+1;
   else
       plot(w1(i1)w1(i2)‘ro‘)
   end
   t=(w2(i:)-M1)*inv(C1)*transpose(w2(i:)-M1);
   d1 = log(0.5)-0.5*log(det(C1))-0.5*t;
   t=(w2(i:)-M2)*inv(C2)*transpose(w2(i:)-M2);
   d2= log(0.5)-0.5*log(det(C2))-0.5*t;
   if d2       plot(w2(i1)w2(i2)‘x‘)
       f2 = f2+1;
   else
       plot(w2(i1)w2(i2)‘o‘)
   end
end
p1 = f1/N*10

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

     文件       3316  2012-12-20 18:43  BYS.m

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

                 3316                    1


评论

共有 条评论