支持向量機作業(yè)_第1頁
支持向量機作業(yè)_第2頁
支持向量機作業(yè)_第3頁
支持向量機作業(yè)_第4頁
支持向量機作業(yè)_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

clcclearcloseall% %%定義核函數(shù)及相關(guān)參數(shù)C=200;%拉格朗日乘子上界ker=struct('type','linea。;%ker=struct('type','ploy','degree',3,'offset',1);%ker=struct('type','gauss','width',1);%ker=struct('type','tanh','gamma',1,'offset',0);%ker-核參數(shù)(結(jié)構(gòu)體變量)%thefollowingfields:%type-linear:k(x,y)=x'*y%poly:k(x,y)=(x'*y+c)Ad%gauss:k(x,y)=exp(-0.5*(norm(x-y)/s)A2)%tanh:k(x,y)=tanh(g*x'*y+c)%degree-Degreedofpolynomialkernel(positivescalar).%offset-Offsetcofpolynomialandtanhkernel(scalar,negativefortanh).%width-WidthsofGausskernel(positivescalar).%gamma-Slopegofthetanhkernel(positivescalar).% %%構(gòu)造兩類訓(xùn)練樣本n=50;randn('state',3);x1=randn(n,2);y1=ones(n,1);x2=5+randn(n,2);y2=-ones(n,1);figure(1);plot(x1(:,1),x1(:,2),'bx',x2(:,1),x2(:,2),'k.');holdon;X=[x1;x2];%訓(xùn)練樣本,n対的矩陣,n為樣本個數(shù),d為樣本維數(shù)Y=[y1;y2];%訓(xùn)練目標(biāo),nM的矩陣,n為樣本個數(shù),值為+1或-1% %%訓(xùn)練支持向量機ticsvm=C_SVC_Train(X,Y,C,ker);t_train=toc%svm支持向量機(結(jié)構(gòu)體變量)%thefollowingfields:%ker-核參數(shù)%x-訓(xùn)練樣本%y-訓(xùn)練目標(biāo);%a-拉格朗日乘子% %%尋找支持向量a=svm.a;epsilon=1e-8;%如果小于此值則認(rèn)為是 0i_sv=find(a>epsilon);%支持向量下標(biāo)plot(X(i_sv,1),X(i_sv,2),'ro');% %%測試輸出[x1,x2]=meshgrid(-2:0.05:7,-2:0.05:7);[rows,cols]=size(x1);nt=rows*cols;%測試樣本數(shù)Xt=[reshape(x1,nt,1),reshape(x2,nt,1)];ticYd=C_SVC_Sim(svm,Xt);% 測試輸出t_sim=tocYd=reshape(Yd,rows,cols);contour(x1,x2,Yd,[00],'m');% 分類面holdoff;function[K]=CalcKernel(ker,x,y)%Calculatekernelfunction.%%x:輸入樣本,n1述的矩陣,n1為樣本個數(shù),d為樣本維數(shù)%y:輸入樣本,n2d的矩陣,n2為樣本個數(shù),d為樣本維數(shù)%%ker核參數(shù)(結(jié)構(gòu)體變量)%thefollowingfields:%type-linear:k(x,y)=x'*y%poly:k(x,y)=(x'*y+c)Ad%gauss:k(x,y)=exp(-0.5*(norm(x-y)/s)A2)%tanh:k(x,y)=tanh(g*x'*y+c)%degree-Degreedofpolynomialkernel(positivescalar).%offset-Offsetcofpolynomialandtanhkernel(scalar,negativefortanh).%width-WidthsofGausskernel(positivescalar).%gamma-Slopegofthetanhkernel(positivescalar).%%ker=struct('type','linear');%ker=struct('type','ploy','degree',d,'offset',c);%ker=struct('type','gauss','width',s);%ker=struct('type','tanh','gamma',g,'offset',c);%%K:輸出核參數(shù),n1n2的矩陣% %%轉(zhuǎn)成列向量x=X:y=y:% %switchker.typecase'linear'K=x'*y;case'ploy'd=ker.degree;c=ker.offset;K=(x'*y+c).Ad;case'gauss's=ker.width;rows=size(x,2);cols=size(y,2);tmp=zeros(rows,cols);fori=1:rowsforj=1:colstmp(i,j)=norm(x(:,i)-y(:,j));endendK=exp(-0.5*(tmp/s).A2);case'tanh'g=ker.gamma;c=ker.offset;K=tanh(g*x'*y+c);otherwiseK=0;endfunctionsvm=C_SVC_Train(X,Y,C,ker)%輸入?yún)?shù):%X訓(xùn)練樣本,n対的矩陣,n為樣本個數(shù),d為樣本維數(shù)%Y訓(xùn)練目標(biāo),nM的矩陣,n為樣本個數(shù),值為+1或-1%C拉格朗日乘子上界%ker核參數(shù)(結(jié)構(gòu)體變量)%thefollowingfields:%type-linear:k(x,y)=x'*y%poly:k(x,y)=(x'*y+c)Ad%gauss:k(x,y)=exp(-0.5*(norm(x-y)/s)A2)%tanh:k(x,y)=tanh(g*x'*y+c)%degree-Degreedofpolynomialkernel(positivescalar).%offset-Offsetcofpolynomialandtanhkernel(scalar,negativefortanh).%width-WidthsofGausskernel(positivescalar).%gamma-Slopegofthetanhkernel(positivescalar).%輸出參數(shù):%svm支持向量機(結(jié)構(gòu)體變量)%thefollowingfields:%ker-核參數(shù)%x-訓(xùn)練樣本%y-訓(xùn)練目標(biāo);%a-拉格朗日乘子% %%解二次優(yōu)化n=length(Y);H=(Y*Y').*Calckernel(ker,X,X);f=-ones(n,1);A=[];b=[];Aeq=Y';beq=0;lb=zeros(n,1);ub=C*ones(n,1);a0=zeros(n,1);options=optimset;options.LargeScale='off;options.Display='off;[a,fval,eXitflag,output,lambda]=quadprog(H,f,A,b,Aeq,beq,lb,ub,aO,options);eXitflag% %%輸出svmsvm.ker=ker;svm.x=X;svm.y=Y;svm.a=a;functionYd=C_SVC_Sim(svm,Xt)%輸入?yún)?shù):%svm支持向量機(結(jié)構(gòu)體變量)%thefollowingfields:%ker-核參數(shù)%type-linear:k(x,y)=x'*y%poly:k(x,y)=(x'*y+c)Ad%gauss:k(x,y)=exp(-0.5*(norm(x-y)/s)A2)%tanh:k(x,y)=tanh(g*x'*y+c)%degree-Degreedofpolynomialkernel(positivescalar).%offset-Offsetcofpolynomialandtanhkernel(scalar,negativefortanh).%width-WidthsofGausskernel(positivescalar).%gamma-Slopegofthetanhkernel(positivescalar).%x-訓(xùn)練樣本%y-訓(xùn)練目標(biāo);%a-拉格朗日乘子%%Xt測試樣本,n:d的矩陣,n為樣本個數(shù),d為樣本維數(shù)%輸出參數(shù):%Yd測試輸出,nM的矩陣,n為樣本個數(shù),值為+1或-1% %ker=svm.ker;X=svm.x;Y=svm.y;a=svm.a;% %%求bepsilon=1e-8;%如果小于此值則認(rèn)為是 0i_sv=find(a>epsilon);%支持向量下標(biāo)tmp=(Y.*a)'*Calckernel(ker,X,X(i_sv,);%行向量b=1./Y(i_sv)-tmp';b=mean(b);% %%測試輸出nt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論