carlpc

SVM交叉验证的代码

0
阅读(2556)
function [bestacc,bestc,bestg] = SVMcg(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,accstep) if nargin < 10 accstep = 1.5; end if nargin < 7 accstep = 1.5; v = 3; cstep = 1; gstep = 1; end if nargin < 6 accstep = 1.5; v = 3; cstep = 1; gstep = 1; gmax = 5; end if nargin < 5 accstep = 1.5; v = 3; cstep = 1; gstep = 1; gmax = 5; gmin = -5; end if nargin < 4 accstep = 1.5; v = 3; cstep = 1; gstep = 1; gmax = 5; gmin = -5; cmax = 5; end if nargin < 3 accstep = 1.5; v = 3; cstep = 1; gstep = 1; gmax = 5; gmin = -5; cmax = 5; cmin = -5; end %% X:c Y:g cg:acc [X,Y] = meshgrid(cmin:cstep:cmax,gmin:gstep:gmax); [m,n] = size(X); cg = zeros(m,n); %% record acc with different c & g,and find the bestacc with the smallest c bestc = 0; bestg = 0; bestacc = 0; basenum = 2; for i = 1:m for j = 1:n cmd = ['-v ',num2str(v),' -c ',num2str( basenum^X(i,j) ),' -g ',num2str( basenum^Y(i,j) )]; cg(i,j) = svmtrain(train_label, train, cmd); if cg(i,j) > bestacc bestacc = cg(i,j); bestc = basenum^X(i,j); bestg = basenum^Y(i,j); end if ( cg(i,j) == bestacc && bestc > basenum^X(i,j) ) bestacc = cg(i,j); bestc = basenum^X(i,j); bestg = basenum^Y(i,j); end end end %% to draw the acc with different c & g figure; [C,h] = contour(X,Y,cg,60:accstep:100); clabel(C,h,'FontSize',10,'Color','r'); xlabel('log2c','FontSize',10); ylabel('log2g','FontSize',10); grid on;


Baidu
map