说明:
此工具箱是matlab中文论坛版主faruto在林智仁教授版本上改进的libsvm工具箱。
工具箱下载地址:http://www.matlabsky.com/thread-17936-1-1.html
本文主要介绍其中增加的参数(c和RBF核函数中的g)寻优函数和其他一些有用的函数。
一、Grid Search 网格搜索
(1)函数:SVMcgForClass.m (分类问题寻优)、SVMcgForRegress.m(回归问题参数寻优)。
详细内容见:http://www.matlabsky.com/thread-12411-1-1.html
(2)使用方法
[bestCVaccuracy,bestc,bestg]= SVMcgForClass(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,accstep)
[bestCVmse,bestc,bestg]= SVMcgForRegress(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,msestep)
2、启发式遗传算法GA
(1)函数: gaSVMcgForClass.m(分类问题参数寻优)、gaSVMcgForRegress.m(回归问题参数寻优)
详情:http://www.matlabsky.com/thread-12412-1-1.html
(2)使用方法
默认范围:c(0.1-100)、g(0.01-1000)、5折交叉验证。
[bestCVaccuracy,bestc,bestg,ga_option]= gaSVMcgForClass(train_label,train,ga_option)
常用格式:[bestCVaccuracy,bestc,bestg] = gaSVMcgForClass(train_label,train)
[bestCVmse,bestc,bestg,ga_option]= gaSVMcgForRegress(train_label,train,ga_option)
若想修改某些默认项比如c、g的范围,可以在原文件中直接修改。
或者ga_option.cbound=[]; ……
(3)参数详解
ga_option:GA中的一些参数设置,可不输入(即去掉等号两边或左边的ga_option),有默认值,详细请看代码的帮助说明。
ga_option =struct('maxgen',200,'sizepop',20,'ggap',0.9,'cbound',[0,100],'gbound',[0,1000],'v',5);
1. % maxgen:最大的进化代数,默认为200,一般取值范围为[100,500]
2. % sizepop:种群最大数量,默认为20,一般取值范围为[20,100]
3. % cbound = [cmin,cmax],参数c的变化范围,默认为(0,100]
4. % gbound = [gmin,gmax],参数g的变化范围,默认为[0,1000]
5. % v:SVM Cross Validation参数,默认为5
3、粒子群算法PSO
(1)函数:psoSVMcgForClass.m(分类问题参数寻优)、psoSVMcgForRegress.m(回归问题参数寻优)
详情:http://www.matlabsky.com/thread-12414-1-1.html
(2)使用方法
默认范围:c(0.1-100)、g(0.01-1000)、3折交叉验证。
适应度函数:分类问题适应度函数用的是cv下的分类准确率;回归问题适应度函数用的是cv下的mse。即fitness(j) = svmtrain(train_label, train, cmd);
[bestCVaccuracy,bestc,bestg,pso_option]= psoSVMcgForClass(train_label,train,pso_option)
常用格式为:[bestCVaccuracy,bestc,bestg]= psoSVMcgForClass(train_label,train)
[bestCVmse,bestc,bestg,pso_option]=
psoSVMcgForRegress(train_label,train,pso_option)
若想修改某些默认项比如c、g的范围,可以在原文件中直接修改。
或者在使用该函数之前:pso_option.popcmax=10000; ……
(3)参数详解
pso_option =struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...
'k',0.6,'wV',1,'wP',1,'v',5,...
'popcmax',10^2,'popcmin',10^(-1),'popgmax',10^3,'popgmin',10^(-2));
1. % c1:初始为1.5,pso参数局部搜索能力
2. % c2:初始为1.7,pso参数全局搜索能力
3. % maxgen:初始为200,最大进化数量
4. % sizepop:初始为20,种群最大数量
5. % k:初始为0.6(k belongs to [0.1,1.0]),速率和x的关系(V = kX)
6. % wV:初始为1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的弹性系数
7. % wP:初始为1,种群更新公式中速度前面的弹性系数
8. % v:初始为3,SVM Cross Validation参数
9. % popcmax:初始为100,SVM参数c的变化的最大值.
10.% popcmin:初始为0.1,SVM参数c的变化的最小值.
11.% popgmax:初始为1000,SVM参数g的变化的最大值.
12. % popgmin:初始为0.01,SVM参数c的变化的最小值.
4、辅助函数
ClassResult.m函数,方便给出各种分类准确率,以及给出判别函数的权值w、偏置b、支持向量在原始训练集中的位置索引以及alpha系数。
格式:
CR = ClassResult(label, data, model,type)
% type 1:输入的data为训练集
% 2:输入的data为测试集
%当输入的data为训练集时,需要事先在外部将标签转换为1,-1
出现的问题及解决措施:20170626更新
运行问题:
>> model = svmtrain(output_train,inputtrain','-s 0 -t 2 -c bestc -g bestg');
Error: C <= 0
解决方法:
1、分开运行,将SVMcgForClass得到的值直接写在svmtrain中。
[bestCVaccuracy,bestc,bestg] = SVMcgForClass(output_train,inputtrain',10,15,-8,8,5);
model = svmtrain(output_train,inputtrain','-s 0 -t 2 -c 16384 -g 0.5743');
注意:改变c和g的范围,可以影响最终分类准确率。
此法比较“笨”。2、
%grid search 参数优化
[bestCVaccuracy,bestc,bestg] = SVMcgForClass(output_train,inputtrain',0,7,-8,8,5);
cmd = ['-c ',num2str(bestc),' -g ',num2str(bestg),'-s 0 -t 2'];
model = svmtrain(output_train,inputtrain',cmd)
即在中间加一步格式的转换。