資源描述:
《fisher分類器設(shè)計》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、模式識別中Fisher分類器的Matlab實(shí)現(xiàn)及測試Fisher分類器用于解決二類線性可分問題。Fisher準(zhǔn)則基本原理:找到一個最合適的投影軸,使兩類樣本在該軸上投影之間的距離盡可能遠(yuǎn),而每一類樣本的投影盡可能緊湊,從而使分類效果為最佳。例如上圖中:通過將方塊點(diǎn)和圓點(diǎn)向w1投影,然后再在設(shè)置合適的閾值即可將方塊和圓點(diǎn)分離。?Matlab程序如下:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ByShelleyfromNCUT,April2nd2011%Email:just_for_h2
2、64@163.com%本m文件實(shí)現(xiàn)fisher算法,并對兩個二維正態(tài)分布隨機(jī)序列%進(jìn)行訓(xùn)練,進(jìn)而可在屏幕上任意取點(diǎn),程序可輸出屬于第一類%還是第二類%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%分別產(chǎn)生x軸和y軸都為正態(tài)分布的隨機(jī)序列%假設(shè)x軸和y軸序列相互獨(dú)立,可產(chǎn)生二維正態(tài)分布隨機(jī)序列%w1、w2分別用來保存兩個訓(xùn)練集的橫坐標(biāo)和縱坐標(biāo)%用normrnd函數(shù)產(chǎn)生正態(tài)分布函數(shù)%normr
3、nd(mean,omega,[row,column])%mean:均值;omega:標(biāo)準(zhǔn)差%row:產(chǎn)生隨機(jī)序列的行數(shù);column:產(chǎn)生隨機(jī)序列的列數(shù)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%?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算法的實(shí)現(xiàn)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%計算樣本均值m1=mean(w1)';m2=mean(w2)';%s1、s2分別代表表示第一類、第二類樣本的類內(nèi)離散度矩陣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;%計算總類內(nèi)離散度矩陣SwSw=s1+s2;%計算fisher準(zhǔn)則函數(shù)取極大值時的解ww=inv(Sw)*(m1-m2);%計算閾值w0ave_m1=w'*m1;ave_m2=w'*m2;w0=(ave_m1+ave_m2)/2;%畫出兩類訓(xùn)練樣本點(diǎn)figure(1)plot(X1,Y1,'.r',X2,Y2,'.b');%畫出兩類樣本點(diǎn)holdon;grid;%畫出取極大值時的解wx=[-40:0.1:40];y
6、=x*w(2)/w(1);plot(x,y,'g')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%以下為測試部分%利用ginput隨機(jī)選取屏幕上的點(diǎn)(可連續(xù)取10個點(diǎn))%程序可根據(jù)點(diǎn)的位置自動地顯示出屬于那個類%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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