资源描述:
《matlab30个案例分析案例13-svm神经网络中的参数优化》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、%%SVM神经网络中的参数优化---如何更好的提升分类器的性能%%清空环境变量functionchapter13_GAcloseall;clear;clc;formatcompact;%%数据提取%载入测试数据wine,其中包含的数据为classnumber=3,wine:178*13的矩阵,wine_labes:178*1的列向量loadchapter13_wine.mat;%画出测试数据的box可视化图figure;boxplot(wine,'orientation','horizontal','labels',categories);title('wine
2、数据的box可视化图','FontSize',12);xlabel('属性值','FontSize',12);gridon;%画出测试数据的分维可视化图figuresubplot(3,5,1);holdonforrun=1:178plot(run,wine_labels(run),'*');endxlabel('样本','FontSize',10);ylabel('类别标签','FontSize',10);title('class','FontSize',10);forrun=2:14subplot(3,5,run);holdon;str=['attrib',n
3、um2str(run-1)];fori=1:178plot(i,wine(i,run-1),'*');endxlabel('样本','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_labe
4、ls(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,nt
5、rain]=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参
6、数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%
7、%Bestc=%gBestg=%g',bestacc,bestc,bestg);disp(str);%%利用最佳的参数进行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);
8、right=sum(pr