Fisher准则线性分类器设计.docx
《Fisher准则线性分类器设计.docx》由会员分享,可在线阅读,更多相关《Fisher准则线性分类器设计.docx(21页珍藏版)》请在冰豆网上搜索。
![Fisher准则线性分类器设计.docx](https://file1.bdocx.com/fileroot1/2022-12/14/33c5fd01-93f1-4f56-9f4b-defc212b1ca8/33c5fd01-93f1-4f56-9f4b-defc212b1ca81.gif)
Fisher准则线性分类器设计
一、基于Fisher准则线性分类器设计
1、实验内容:
已知有两类数据
和
二者的概率已知
=0.6,
=0.4。
中数据点的坐标对应一一如下:
数据:
x=
0.23311.52070.64990.77571.05241.1974
0.29080.25180.66820.56220.90230.1333
-0.54310.9407-0.21260.0507-0.08100.7315
0.33451.0650-0.02470.10430.31220.6655
0.58381.16531.26530.8137-0.33990.5152
0.7226-0.20150.4070-0.1717-1.0573-0.2099
y=
2.33852.19461.67301.63651.78442.0155
2.06812.12132.47971.51181.96921.8340
1.87042.29481.77142.39391.56481.9329
2.20272.45681.75231.69912.48831.7259
2.04662.02262.37571.79872.08282.0798
1.94492.38012.23732.16141.92352.2604
z=
0.53380.85141.08310.41641.11760.5536
0.60710.44390.49280.59011.09271.0756
1.00720.42720.43530.98690.48411.0992
1.02990.71271.01240.45760.85441.1275
0.77050.41291.00850.76760.84180.8784
0.97510.78400.41581.03150.75330.9548
数据点的对应的三维坐标为
x2=
1.40101.23012.08141.16551.37401.1829
1.76321.97392.41522.58902.84721.9539
1.25001.28641.26142.00712.18311.7909
1.33221.14661.70871.59202.93531.4664
2.93131.83491.83402.50962.71982.3148
2.03532.60301.23272.14651.56732.9414
y2=
1.02980.96110.91541.49010.82000.9399
1.14051.06780.80501.28891.46011.4334
0.70911.29421.37440.93871.22661.1833
0.87980.55920.51500.99830.91200.7126
1.28331.10291.26800.71401.24461.3392
1.18080.55031.47081.14350.76791.1288
z2=
0.62101.36560.54980.67080.89321.4342
0.95080.73240.57841.49431.09150.7644
1.21591.30491.14080.93980.61970.6603
1.39281.40840.69090.84000.53811.3729
0.77310.73191.34390.81420.95860.7379
0.75480.73930.67390.86511.36991.1458
数据的样本点分布如下图:
1)请把数据作为样本,根据Fisher选择投影方向
的原则,使原样本向量在该方向上的投影能兼顾类间分布尽可能分开,类内样本投影尽可能密集的要求,求出评价投影方向
的函数,并在图形表示出来。
并在实验报告中表示出来,并求使
取极大值的
。
用matlab完成Fisher线性分类器的设计,程序的语句要求有注释。
2)根据上述的结果并判断(1,1.5,0.6)(1.2,1.0,0.55),(2.0,0.9,0.68),(1.2,1.5,0.89),(0.23,2.33,1.43),属于哪个类别,并画出数据分类相应的结果图,要求画出其在
上的投影。
3)回答如下问题,分析一下
的比例因子对于Fisher判别函数没有影响的原因。
2、实验代码
x1=[0.23311.52070.64990.77571.05241.1974
0.29080.25180.66820.56220.90230.1333
-0.54310.9407-0.21260.0507-0.08100.7315
0.33451.0650-0.02470.10430.31220.6655
0.58381.16531.26530.8137-0.33990.5152
0.7226-0.20150.4070-0.1717-1.0573-0.2099];
x2=[2.33852.19461.67301.63651.78442.0155
2.06812.12132.47971.51181.96921.8340
1.87042.29481.77142.39391.56481.9329
2.20272.45681.75231.69912.48831.7259
2.04662.02262.37571.79872.08282.0798
1.94492.38012.23732.16141.92352.2604];
x3=[0.53380.85141.08310.41641.11760.5536
0.60710.44390.49280.59011.09271.0756
1.00720.42720.43530.98690.48411.0992
1.02990.71271.01240.45760.85441.1275
0.77050.41291.00850.76760.84180.8784
0.97510.78400.41581.03150.75330.9548];
%将x1、x2、x3变为行向量
x1=x1(:
);x2=x2(:
);x3=x3(:
);
%计算第一类的样本均值向量m1
m1
(1)=mean(x1);
m1
(2)=mean(x2);
m1(3)=mean(x3);
%计算第一类样本类内离散度矩阵S1
S1=zeros(3,3);
fori=1:
36
S1=S1+[-m1
(1)+x1(i)-m1
(2)+x2(i)-m1(3)+x3(i)]'*[-m1
(1)+x1(i)-m1
(2)+x2(i)-m1(3)+x3(i)];
end
%w2的数据点坐标
x4=[1.40101.23012.08141.16551.37401.1829
1.76321.97392.41522.58902.84721.9539
1.25001.28641.26142.00712.18311.7909
1.33221.14661.70871.59202.93531.4664
2.93131.83491.83402.50962.71982.3148
2.03532.60301.23272.14651.56732.9414];
x5=[1.02980.96110.91541.49010.82000.9399
1.14051.06780.80501.28891.46011.4334
0.70911.29421.37440.93871.22661.1833
0.87980.55920.51500.99830.91200.7126
1.28331.10291.26800.71401.24461.3392
1.18080.55031.47081.14350.76791.1288];
x6=[0.62101.36560.54980.67080.89321.4342
0.95080.73240.57841.49431.09150.7644
1.21591.30491.14080.93980.61970.6603
1.39281.40840.69090.84000.53811.3729
0.77310.73191.34390.81420.95860.7379
0.75480.73930.67390.86511.36991.1458];
x4=x4(:
);
x5=x5(:
);
x6=x6(:
);
%计算第二类的样本均值向量m2
m2
(1)=mean(x4);
m2
(2)=mean(x5);
m2(3)=mean(x6);
%计算第二类样本类内离散度矩阵S2
S2=zeros(3,3);
fori=1:
36
S2=S2+[-m2
(1)+x4(i)-m2
(2)+x5(i)-m2(3)+x6(i)]'*[-m2
(1)+x4(i)-m2
(2)+x5(i)-m2(3)+x6(i)];
end
%总类内离散度矩阵Sw
Sw=zeros(3,3);
Sw=S1+S2;
%样本类间离散度矩阵Sb
Sb=zeros(3,3);
Sb=(m1-m2)'*(m1-m2);
%最优解W
W=Sw^-1*(m1-m2)'
%将W变为单位向量以方便计算投影
W=W/sqrt(sum(W.^2));
%计算一维Y空间中的各类样本均值M1及M2
fori=1:
36
y(i)=W'*[x1(i)x2(i)x3(i)]';
end
M1=mean(y);
fori=1:
36
y(i)=W'*[x4(i)x5(i)x6(i)]';
end
M2=mean(y);
%利用当P(w1)与P(w2)已知时的公式计算W0
p1=0.6;p2=0.4;
W0=-(M1+M2)/2+(log(p2/p1))/(36+36-2);
%计算将样本投影到最佳方向上以后的新坐标
X1=[x1*W
(1)+x2*W
(2)+x3*W(3)]';
X2=[x4*W
(1)+x5*W
(2)+x6*W(3)]';%得到投影长度
XX1=[W
(1)*X1;W
(2)*X1;W(3)*X1];
XX2=[W
(1)*X2;W
(2)*X2;W(3)*X2];%得到新坐标
%绘制样本点
figure
(1);
plot3(x1,x2,x3,'r*');%第一类
holdon
plot3(x4,x5,x6,'gp');%第二类
legend('第一类点','第二类点');
title('Fisher线性判别曲线');
W1=5*W;
%画出最佳方向
line([-W1
(1),W1
(1)],[-W1
(2),W1
(2)],[-W1(3),W1(3)],'color','g');
%判别已给点的分类
a1=[1,1.5,0.6]';a2=[1.2,1.0,0.55]';a3=[2.0,0.9,0.68]';a4=[1.2,1.5,0.89]';a5=[0.23,2.33,1.43]';
A=[a1a2a3a4a5];
n=size(A,2);
%下面代码在改变样本时可不修改
%绘制待测数据投影到最佳方向上的点
fork=1:
n
A1=A(:
k)'*W;
A11=W*A1;%得到待测数据投影
y=W'*A(:
k)+W0;%计算后与0相比以判断类别,大于0为第一类,小于0为第二类
ify>0
plot3(A(1,k),A(2,k),A(3,k),'ro');%点为"rp"对应第一类
plot3(A11
(1),A11
(2),A11(3),'ro');%投影为"r+"对应ro类
else
plot3(A(1,k),A(2,k),A(3,k),'ch');%点为"bh"对应ch类
plot3(A11
(1),A11
(2),A11(3),'ch');%投影为"b*"对应ch类
end
end
%画出最佳方向
line([-W1
(1),W1
(1)],[-W1
(2),W1
(2)],[-W1(3),W1(3)],'color','m');
view([-37.5,30]);
axis([-2,3,-1,3,-0.5,1.5]);
gridon
holdoff
3、实验结果
根据求出最佳投影方向,然后按照此方向,将待测数据进行投影。
为直观起见,我们将两步画在一张图上,如下:
其中,红色的*是给出的第一类样本点,蓝色的五角星是第二类样本点。
下方的实直线是最佳投影方向。
待测数据投影在其上,圆圈是被分为第一类的样本点,十字是被分为第二类的样本点。
使
取极大值的W=(-0.0798,0.2005,-0.0478)
4、实验分析
W的比例因子对于Fisher判别函数没有影响的原因:
在本实验中,最需要的是W的方向,或者说是在此方向上数据的投影,那么W的比例因子,即它是单位向量的多少倍长就无关紧要了,不管比例因子有多大,在最后求投影时都会被消掉而起不到实际作用。
二、Bayes分类器设计
1、实验内容
假定某个局部区域细胞识别中正常(
)和非正常(
)两类先验概率分别为正常状态:
P(
)=0.9;异常状态:
P(
)=0.1。
现有一系列待观察的细胞,其观察值为
:
-3.9847-3.5549-1.2401-0.9780-0.7932-2.8531
-2.7605-3.7287-3.5414-2.2692-3.4549-3.0752
-3.99342.8792-0.97800.79321.18823.0682
-1.5799-1.4885-0.7431-0.4221-1.11864.2532
已知先验概率是的曲线如下图:
类条件概率分布正态分布分别为(-2,0.25)(2,4)试对观察的结果进行分类。
1)用matlab完成分类器的设计,要求程序相应语句有说明文字,要求有子程序的调用过程。
2)根据例子画出后验概率的分布曲线以及分类的结果示意图。
3)如果是最小风险贝叶斯决策,决策表如下:
最小风险贝叶斯决策表:
状态
决策
α1
0
6
α2
1
0
请重新设计程序,画出相应的后验概率的分布曲线和分类结果,并比较两个结果。
2、实验代码
2.1最小错误率贝叶斯决策(m1.m)
x=[-3.9847-3.5549-1.2401-0.9780-0.7932-2.8531
-2.7605-3.7287-3.5414-2.2692-3.4549-3.0752
-3.99342.8792-0.97800.79321.18823.0682
-1.5799-1.48850.7431-0.4221-1.11864.2532]
pw1=0.9;pw2=0.1;
e1=-2;a1=0.5;
e2=2;a2=2;
m=numel(x);%得到待测细胞个数
pw1_x=zeros(1,m);%存放对w1的后验概率矩阵
pw2_x=zeros(1,m);%存放对w2的后验概率矩阵
results=zeros(1,m);%存放比较结果矩阵
fori=1:
m
%计算在w1下的后验概率
pw1_x(i)=(pw1*normpdf(x(i),e1,a1))/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2));
%计算在w2下的后验概率
pw2_x(i)=(pw2*normpdf(x(i),e2,a2))/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2));
end
fori=1:
m
ifpw1_x(i)>pw2_x(i)%比较两类后验概率
result(i)=0;%正常细胞
else
result(i)=1;%异常细胞
end
end
a=[-5:
0.05:
5];%取样本点以画图
n=numel(a);
pw1_plot=zeros(1,n);
pw2_plot=zeros(1,n);
forj=1:
n
pw1_plot(j)=(pw1*normpdf(a(j),e1,a1))/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2));
%计算每个样本点对w1的后验概率以画图
pw2_plot(j)=(pw2*normpdf(a(j),e2,a2))/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2));
end
figure
(1);
holdon
plot(a,pw1_plot,'co',a,pw2_plot,'r-.');
fork=1:
m
ifresult(k)==0
plot(x(k),-0.1,'cp');%正常细胞用五角星表示
else
plot(x(k),-0.1,'r*');%异常细胞用*表示
end;
end;
legend('正常细胞后验概率曲线','异常细胞后验概率曲线','正常细胞','异常细胞');
xlabel('样本细胞的观察值');
ylabel('后验概率');
title('后验概率分布曲线');
gridon
return
%实验内容仿真:
x=[-3.9847,-3.5549,-1.2401,-0.9780,-0.7932,-2.8531,-2.7605,-3.7287,-3.5414,-2.2692,-3.4549,-3.075,-3.9934,2.8792,-0.9780,0.7932,1.1882,3.0682,-1.5799,-1.4885,-0.7431,-0.4221,-1.1186,4.2532]
disp(x);
pw1=0.9;
pw2=0.1;
[result]=bayes(x,pw1,pw2);
2.2最小风险贝叶斯决策(m2.m)
x=[-3.9847-3.5549-1.2401-0.9780-0.7932-2.8531
-2.7605-3.7287-3.5414-2.2692-3.4549-3.0752
-3.99342.8792-0.97800.79321.18823.0682
-1.5799-1.48850.7431-0.4221-1.11864.2532]
pw1=0.9;pw2=0.1;
m=numel(x);%得到待测细胞个数
R1_x=zeros(1,m);%存放把样本X判为正常细胞所造成的整体损失
R2_x=zeros(1,m);%存放把样本X判为异常细胞所造成的整体损失
result=zeros(1,m);%存放比较结果
e1=-2;a1=0.5;
e2=2;a2=2;
%类条件概率分布px_w1:
(-2,0.25)px_w2(2,4)
r11=0;r12=2;
r21=4;r22=0;
%风险决策表
fori=1:
m
%计算两类风险值
R1_x(i)=r11*pw1*normpdf(x(i),e1,a1)/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2))+r21*pw2*normpdf(x(i),e2,a2)/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2));
R2_x(i)=r12*pw1*normpdf(x(i),e1,a1)/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2))+r22*pw2*normpdf(x(i),e2,a2)/(pw1*normpdf(x(i),e1,a1)+pw2*normpdf(x(i),e2,a2));
end
fori=1:
m
ifR2_x(i)>R1_x(i)%第二类比第一类风险大
result(i)=0;%判为正常细胞(损失较小),用0表示
else
result(i)=1;%判为异常细胞,用1表示
end
end
a=[-5:
0.05:
5];%取样本点以画图
n=numel(a);
R1_plot=zeros(1,n);
R2_plot=zeros(1,n);
forj=1:
n
R1_plot(j)=r11*pw1*normpdf(a(j),e1,a1)/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2))+r21*pw2*normpdf(a(j),e2,a2)/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2))
R2_plot(j)=r12*pw1*normpdf(a(j),e1,a1)/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2))+r22*pw2*normpdf(a(j),e2,a2)/(pw1*normpdf(a(j),e1,a1)+pw2*normpdf(a(j),e2,a2))
%计算各样本点的风险以画图
end
figure
(1);
holdon
plot(a,R1_plot,'co',a,R2_plot,'r-.');
fork=1:
m
ifresult(k)==0
plot(x(k),-0.1,'cp');%正常细胞用五角星表示
else
plot(x(k),-0.1,'r*');%异常细胞用*表示
end;
end;
legend('正常细胞','异常细胞','Location','Best');
xlabel('细胞分类结果');
ylabel('条件风险');
title('风险判决曲线');
gridon
return
%实验内容仿真:
x=[-3.9847,-3.5549,-1.2401,-0.9780,-0.7932,-2.8531,-2.7605,-3.7287,-3.5414,-2.2692,-3.4549,-3.075,-3.9934