资源描述:
《fisher分类器设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、模式识别中Fisher分类器的Matlab实现及测试Fisher分类器用于解决二类线性可分问题。Fisher准则基本原理:找到一个最合适的投影轴,使两类样本在该轴上投影之间的距离尽可能远,而每一类样本的投影尽可能紧凑,从而使分类效果为最佳。例如上图中:通过将方块点和圆点向w1投影,然后再在设置合适的阈值即可将方块和圆点分离。 Matlab程序如下:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ByShelleyfromNCUT,April2nd2011%Email:just_for_h2
2、64@163.com%本m文件实现fisher算法,并对两个二维正态分布随机序列%进行训练,进而可在屏幕上任意取点,程序可输出属于第一类%还是第二类%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%分别产生x轴和y轴都为正态分布的随机序列%假设x轴和y轴序列相互独立,可产生二维正态分布随机序列%w1、w2分别用来保存两个训练集的横坐标和纵坐标%用normrnd函数产生正态分布函数%normr
3、nd(mean,omega,[row,column])%mean:均值;omega:标准差%row:产生随机序列的行数;column:产生随机序列的列数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% X1=normrnd(40,10,[200,1]); Y1=normrnd(40,10,[200,1]); w1=[X1,Y1]; X2=normrnd(5,10,[100,1]); Y2=normrnd(0,10,[100,1]); w2=[X2,Y2]; %%%%%%%%%%%%%%%%%%
4、%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%以下部分为fisher算法的实现%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算样本均值m1=mean(w1)';m2=mean(w2)';%s1、s2分别代表表示第一类、第二类样本的类内离散度矩阵s1=zeros(2);[row1,colum1]=size(w1);fori=1:row1 s1=s1+(w1(i,:)'-m1)*(w1(i,:)'-m1)';end;s2=zeros(2);[row2,colum
5、2]=size(w2);fori=1:row2 s2=s2+(w2(i,:)'-m2)*(w2(i,:)'-m2)';end;%计算总类内离散度矩阵SwSw=s1+s2;%计算fisher准则函数取极大值时的解ww=inv(Sw)*(m1-m2);%计算阈值w0ave_m1=w'*m1;ave_m2=w'*m2;w0=(ave_m1+ave_m2)/2;%画出两类训练样本点figure(1)plot(X1,Y1,'.r',X2,Y2,'.b');%画出两类样本点holdon;grid;%画出取极大值时的解wx=[-40:0.1:40];y
6、=x*w(2)/w(1);plot(x,y,'g')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%以下为测试部分%利用ginput随机选取屏幕上的点(可连续取10个点)%程序可根据点的位置自动地显示出属于那个类%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%fori=1:10 [x,y]=ginput(1); plot(x,y,'m*'); sample=[x,y]; holdall if(sample*w-w0>
7、0) disp('itbelongtothefirstclass'); else disp('itbelongtothesecondclass'); end;end