MATLAB 神经网络SVM的参数优化如何更好的提升分类器的性能.docx
- 文档编号:4395904
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:27
- 大小:182.33KB
MATLAB 神经网络SVM的参数优化如何更好的提升分类器的性能.docx
《MATLAB 神经网络SVM的参数优化如何更好的提升分类器的性能.docx》由会员分享,可在线阅读,更多相关《MATLAB 神经网络SVM的参数优化如何更好的提升分类器的性能.docx(27页珍藏版)》请在冰豆网上搜索。
MATLAB神经网络SVM的参数优化如何更好的提升分类器的性能
清空环境变量
functionchapter_GA
closeall;
clear;
clc;
formatcompact;
数据提取
%载入测试数据wine,其中包含的数据为classnumber=3,wine:
178*13的矩阵,wine_labes:
178*1的列向量
loadwine.mat;
%画出测试数据的box可视化图
figure;
boxplot(wine,'orientation','horizontal','labels',categories);
title('wine数据的box可视化图','FontSize',12);
xlabel('属性值','FontSize',12);
gridon;
%画出测试数据的分维可视化图
figure
subplot(3,5,1);
holdonforrun=1:
178
plot(run,wine_labels(run),'*');end
xlabel('样本','FontSize',10);
ylabel('类别标签','FontSize',10);
title('class','FontSize',10);forrun=2:
14
subplot(3,5,run);
holdon;
str=['attrib',num2str(run-1)];
fori=1:
178
plot(i,wine(i,run-1),'*');
end
xlabel('样本','FontSize',10);
ylabel('属性值','FontSize',10);
title(str,'FontSize',10);end
%选定训练集和测试集
%将第一类的1-30,第二类的60-95,第三类的131-153做为训练集
train_wine=[wine(1:
30,:
);wine(60:
95,:
);wine(131:
153,:
)];%相应的训练集的标签也要分离出来
train_wine_labels=[wine_labels(1:
30);wine_labels(60:
95);wine_labels(131:
153)];%将第一类的31-59,第二类的96-130,第三类的154-178做为测试集
test_wine=[wine(31:
59,:
);wine(96:
130,:
);wine(154:
178,:
)];%相应的测试集的标签也要分离出来
test_wine_labels=[wine_labels(31:
59);wine_labels(96:
130);wine_labels(154:
178)];
数据预处理
数据预处理,将训练集和测试集归一化到[0,1]区间
[mtrain,ntrain]=size(train_wine);
[mtest,ntest]=size(test_wine);
dataset=[train_wine;test_wine];%mapminmax为MATLAB自带的归一化函数
[dataset_scale,ps]=mapminmax(dataset',0,1);
dataset_scale=dataset_scale';
train_wine=dataset_scale(1:
mtrain,:
);
test_wine=dataset_scale((mtrain+1):
(mtrain+mtest),:
);
选择GA最佳的SVM参数c&g
%GA的参数选项初始化
ga_option.maxgen=200;
ga_option.sizepop=20;
ga_option.cbound=[0,100];
ga_option.gbound=[0,100];
ga_option.v=5;
ga_option.ggap=0.9;
[bestacc,bestc,bestg]=gaSVMcgForClass(train_wine_labels,train_wine,ga_option);
%打印选择结果
disp('打印选择结果');
str=sprintf('BestCrossValidationAccuracy=%g%%Bestc=%gBestg=%g',bestacc,bestc,bestg);
disp(str);
打印选择结果
BestCrossValidationAccuracy=98.8764%Bestc=4.05092Bestg=3.0839
利用最佳的参数进行SVM网络训练
cmd=['-c',num2str(bestc),'-g',num2str(bestg)];
model=svmtrain(train_wine_labels,train_wine,cmd);
SVM网络预测
[predict_label,accuracy]=svmpredict(test_wine_labels,test_wine,model);
%打印测试集分类准确率
total=length(test_wine_labels);
right=sum(predict_label==test_wine_labels);
disp('打印测试集分类准确率');
str=sprintf('Accuracy=%g%%(%d/%d)',accuracy
(1),right,total);
disp(str);
Accuracy=98.8764%(88/89)(classification)
打印测试集分类准确率
Accuracy=98.8764%(88/89)
结果分析
%测试集的实际分类和预测分类图
figure;
holdon;
plot(test_wine_labels,'o');
plot(predict_label,'r*');
xlabel('测试集样本','FontSize',12);
ylabel('类别标签','FontSize',12);
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',12);
gridon;
snapnow;
子函数gaSVMcgForClass.m
function[BestCVaccuracy,Bestc,Bestg,ga_option]=gaSVMcgForClass(train_label,train_data,ga_option)%gaSVMcgForClass
%%byfaruto%Email:
patrick.lee@QQ:
516667408BNU%lastmodified2010.01.17%SuperModerator@
%若转载请注明:
%farutoandliyang,LIBSVM-farutoUltimateVersion%atoolboxwithimplementsforsupportvectormachinesbasedonlibsvm,2009.%Softwareavailableat%%Chih-ChungChangandChih-JenLin,LIBSVM:
alibraryfor%supportvectormachines,2001.Softwareavailableat%http:
//www.csie.ntu.edu.tw/~cjlin/libsvm
%参数初始化ifnargin==2
ga_option=struct('maxgen',200,'sizepop',20,'ggap',0.9,...
'cbound',[0,100],'gbound',[0,1000],'v',5);end%maxgen:
最大的进化代数,默认为200,一般取值范围为[100,500]%sizepop:
种群最大数量,默认为20,一般取值范围为[20,100]%cbound=[cmin,cmax],参数c的变化范围,默认为(0,100]%gbound=[gmin,gmax],参数g的变化范围,默认为[0,1000]%v:
SVMCrossValidation参数,默认为5
%
MAXGEN=ga_option.maxgen;
NIND=ga_option.sizepop;
NVAR=2;
PRECI=20;
GGAP=ga_option.ggap;
trace=zeros(MAXGEN,2);
FieldID=...
[rep([PRECI],[1,NVAR]);[ga_option.cbound
(1),ga_option.gbound
(1);ga_option.cbound
(2),ga_option.gbound
(2)];...
[1,1;0,0;0,1;1,1]];
Chrom=crtbp(NIND,NVAR*PRECI);
gen=1;
v=ga_option.v;
BestCVaccuracy=0;
Bestc=0;
Bestg=0;%
cg=bs2rv(Chrom,FieldID);
fornind=1:
NIND
cmd=['-v',num2str(v),'-c',num2str(cg(nind,1)),'-g',num2str(cg(nind,2))];
ObjV(nind,1)=svmtrain(train_label,train_data,cmd);end
[BestCVaccuracy,I]=max(ObjV);
Bestc=cg(I,1);
Bestg=cg(I,2);
forgen=1:
MAXGEN
FitnV=ranking(-ObjV);
SelCh=select('sus',Chrom,FitnV,GGAP);
SelCh=recombin('xovsp',SelCh,0.7);
SelCh=mut(SelCh);
cg=bs2rv(SelCh,FieldID);
fornind=1:
size(SelCh,1)
cmd=['-v',num2str(v),'-c',num2str(cg(nind,1)),'-g',num2str(cg(nind,2))];
ObjVSel(nind,1)=svmtrain(train_label,train_data,cmd);
end
[Chrom,ObjV]=reins(Chrom,SelCh,1,1,ObjV,ObjVSel);
ifmax(ObjV)<=50
continue;
end
[NewBestCVaccuracy,I]=max(ObjV);
cg_temp=bs2rv(Chrom,FieldID);
temp_NewBestCVaccuracy=NewBestCVaccuracy;
ifNewBestCVaccuracy>BestCVaccuracy
BestCVaccuracy=NewBestCVaccuracy;
Bestc=cg_temp(I,1);
Bestg=cg_temp(I,2);
end
ifabs(NewBestCVaccuracy-BestCVaccuracy)<=10^(-2)&&...
cg_temp(I,1) BestCVaccuracy=NewBestCVaccuracy; Bestc=cg_temp(I,1); Bestg=cg_temp(I,2); end trace(gen,1)=max(ObjV); trace(gen,2)=sum(ObjV)/length(ObjV); end% figure; holdon; trace=round(trace*10000)/10000; plot(trace(1: gen,1),'r*-','LineWidth',1.5); plot(trace(1: gen,2),'o-','LineWidth',1.5); legend('最佳适应度','平均适应度',3); xlabel('进化代数','FontSize',12); ylabel('适应度','FontSize',12); axis([0gen0100]); gridon; axisauto; line1='适应度曲线Accuracy[GAmethod]'; line2=['(终止代数=',... num2str(gen),',种群数量pop=',... num2str(NIND),')']; line3=['Bestc=',num2str(Bestc),'g=',num2str(Bestg),... 'CVAccuracy=',num2str(BestCVaccuracy),'%']; title({line1;line2;line3},'FontSize',12); 清空环境变量 functionchapter_GridSearch closeall; clear; clc; formatcompact; 数据提取 %载入测试数据wine,其中包含的数据为classnumber=3,wine: 178*13的矩阵,wine_labes: 178*1的列向量 loadwine.mat; %画出测试数据的box可视化图 figure; boxplot(wine,'orientation','horizontal','labels',categories); title('wine数据的box可视化图','FontSize',12); xlabel('属性值','FontSize',12); gridon; %画出测试数据的分维可视化图 figure subplot(3,5,1); holdonforrun=1: 178 plot(run,wine_labels(run),'*');end xlabel('样本','FontSize',10); ylabel('类别标签','FontSize',10); title('class','FontSize',10);forrun=2: 14 subplot(3,5,run); holdon; str=['attrib',num2str(run-1)]; fori=1: 178 plot(i,wine(i,run-1),'*'); end xlabel('样本','FontSize',10); ylabel('属性值','FontSize',10); title(str,'FontSize',10);end %选定训练集和测试集 %将第一类的1-30,第二类的60-95,第三类的131-153做为训练集 train_wine=[wine(1: 30,: );wine(60: 95,: );wine(131: 153,: )];%相应的训练集的标签也要分离出来 train_wine_labels=[wine_labels(1: 30);wine_labels(60: 95);wine_labels(131: 153)];%将第一类的31-59,第二类的96-130,第三类的154-178做为测试集 test_wine=[wine(31: 59,: );wine(96: 130,: );wine(154: 178,: )];%相应的测试集的标签也要分离出来 test_wine_labels=[wine_labels(31: 59);wine_labels(96: 130);wine_labels(154: 178)]; 数据预处理 数据预处理,将训练集和测试集归一化到[0,1]区间 [mtrain,ntrain]=size(train_wine); [mtest,ntest]=size(test_wine); dataset=[train_wine;test_wine];%mapminmax为MATLAB自带的归一化函数 [dataset_scale,ps]=mapminmax(dataset',0,1); dataset_scale=dataset_scale'; train_wine=dataset_scale(1: mtrain,: ); test_wine=dataset_scale((mtrain+1): (mtrain+mtest),: ); 选择最佳的SVM参数c&g %首先进行粗略选择: c&g的变化范围是2^(-10),2^(-9),...,2^(10) [bestacc,bestc,bestg]=SVMcgForClass(train_wine_labels,train_wine,-10,10,-10,10); %打印粗略选择结果 disp('打印粗略选择结果'); str=sprintf('BestCrossValidationAccuracy=%g%%Bestc=%gBestg=%g',bestacc,bestc,bestg); disp(str); %根据粗略选择的结果图再进行精细选择: c的变化范围是2^(-2),2^(-1.5),...,2^(4),g的变化范围是2^(-4),2^(-3.5),...,2^(4), [bestacc,bestc,bestg]=SVMcgForClass(train_wine_labels,train_wine,-2,4,-4,4,3,0.5,0.5,0.9);%打印精细选择结果 disp('打印精细选择结果'); str=sprintf('BestCrossValidationAccuracy=%g%%Bestc=%gBestg=%g',bestacc,bestc,bestg); disp(str); 打印粗略选择结果 BestCrossValidationAccuracy=98.8764%Bestc=2.2974Bestg=4 打印精细选择结果 BestCrossValidationAccuracy=98.8764%Bestc=1.41421Bestg=1 利用最佳的参数进行SVM网络训练 cmd=['-c',num2str(bestc),'-g',num2str(bestg)]; model=svmtrain(train_wine_labels,train_wine,cmd); SVM网络预测 [predict_label,accuracy]=svmpredict(test_wine_labels,test_wine,model); %打印测试集分类准确率 total=length(test_wine_labels); right=sum(predict_label==test_wine_labels); disp('打印测试集分类准确率'); str=sprintf('Accuracy=%g%%(%d/%d)',accuracy (1),right,total); disp(str); Accuracy=98.8764%(88/89)(classification) 打印测试集分类准确率 Accuracy=98.8764%(88/89) 结果分析 %测试集的实际分类和预测分类图%通过图可以看出只有三个测试样本是被错分的 figure; holdon; plot(test_wine_labels,'o'); plot(predict_label,'r*'); xlabel('测试集样本','FontSize',12); ylabel('类别标签','FontSize',12); legend('实际测试集分类','预测测试集分类'); title('测试集的实际分类和预测分类图','FontSize',12); gridon; snapnow; 子函数SVMcgForClass.m function[bestacc,bestc,bestg]=SVMcgForClass(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,accstep)%SVMcgcrossvalidationbyfaruto %%byfaruto%Email: patrick.lee@QQ: 516667408BNU%lastmodified2010.01.17%SuperModerator@ %若转载请注明: %farutoandliyang,LIBSVM-farutoUltimateVersion%atoolboxwithimplementsforsupportvectormachinesbasedonlibsvm,2009.%Softwareavailableat%%Chih-ChungChangandChih-JenLin,LIBSVM: alibraryfor%supportvectormachines,2001.Softwareavailableat%http: //www.csie.ntu.edu.tw/~cjlin/libsvm %abouttheparametersofSVMcgifnargin<10 accstep=4.5;endifnargin<8 cstep=0.8; gstep=0.8;endifnargin<7 v=5;endifnargin<5 gmax=8; gmin=-8;endifnargin<3 cmax=8; cmin=-8;end%X: cY: gcg: CVaccuracy [X,Y]=meshgrid(cmin: cstep: cmax,gmin: gstep: gmax); [m,n]=size(X); cg=zeros(m,n); eps=10^(-4); %recorda
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MATLAB 神经网络SVM的参数优化如何更好的提升分类器的性能 神经网络 SVM 参数 优化 如何 更好 提升 分类 性能
![提示](https://static.bdocx.com/images/bang_tan.gif)