matlab svmtrain 参数,基于GA的svm参数寻优

function [BestCVaccuracy,Bestc,Bestg,ga_option] = gaSVMcgForClass(train_label,train_data,ga_option)

% gaSVMcgForClass

%%

% by faruto

%Email:patrick.lee@foxmail.com QQ:516667408 http://blog.sina.com.cn/faruto BNU

%last modified 2010.01.17

%% 若转载请注明:

% faruto and liyang , LIBSVM-farutoUltimateVersion

% a toolbox with implements for support vector machines based on libsvm, 2009.

%

% Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for

% support vector machines, 2001. Software available at

% http://www.csie.ntu.edu.tw/~cjlin/libsvm

%% 参数初始化

if nargin == 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:SVM Cross Validation参数,默认为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);

for nind = 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);

%%

while 1

% for gen = 1:MAXGEN

FitnV = ranking(-ObjV);

SelCh = select('sus',Chrom,FitnV,GGAP);

SelCh = recombin('xovsp',SelCh,0.7);

SelCh = mut(SelCh);

cg = bs2rv(SelCh,FieldID);

for nind = 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);

if max(ObjV) <= 50

continue;

end

[NewBestCVaccuracy,I] = max(ObjV);

cg_temp = bs2rv(Chrom,FieldID);

temp_NewBestCVaccuracy = NewBestCVaccuracy;

if NewBestCVaccuracy > BestCVaccuracy

BestCVaccuracy = NewBestCVaccuracy;

Bestc = cg_temp(I,1);

Bestg = cg_temp(I,2);

end

if abs( NewBestCVaccuracy-BestCVaccuracy ) <= 10^(-2) && ...

cg_temp(I,1) < Bestc

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);

gen = gen+1;

if gen <= MAXGEN/2

continue;

end

if BestCVaccuracy >=80 && ...

( temp_NewBestCVaccuracy-BestCVaccuracy ) <= 10^(-2)

break;

end

if gen == MAXGEN

break;

end

end

gen = gen -1;

%%

figure;

hold on;

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([0 gen 0 100]);

grid on;

axis auto;

line1 = '适应度曲线Accuracy[GAmethod]';

line2 = ['(终止代数=', ...

num2str(gen),',种群数量pop=', ...

num2str(NIND),')'];

line3 = ['Best c=',num2str(Bestc),' g=',num2str(Bestg), ...

' CVAccuracy=',num2str(BestCVaccuracy),'%'];

title({line1;line2;line3},'FontSize',12);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值