How can I use to select parameters?

I want to select c and gamma options for C-SVM classification using RBF core (radial basis function) with libsvm \ tools \ but I don't know how this is possible? i installed libsvm and gnuplot and python and ran in python but it had an error and no results were shown.


source to share

2 answers

%grid of parameters
folds = 5; 
[C,gamma] = meshgrid(-5:2:15, -15:2:3); 
%# grid search, and cross-validation 
cv_acc = zeros(numel(C),1); 
d= 2;
for i=1:numel(C)   
    cv_acc(i) = svmtrain(TrainLabel,TrainVec, ...          
        sprintf('-c %f -g %f -v %d -t %d', 2^C(i), 2^gamma(i), folds,d));
%# pair (C,gamma) with best accuracy
[~,idx] = max(cv_acc); 
%# contour plot of paramter selection 
contour(C, gamma, reshape(cv_acc,size(C))), colorbar
hold on;
text(C(idx), gamma(idx), sprintf('Acc = %.2f %%',cv_acc(idx)), ...  
    'HorizontalAlign','left', 'VerticalAlign','top') 
hold off 
xlabel('log_2(C)'), ylabel('log_2(\gamma)'), title('Cross-Validation Accuracy') 
%# now you can train you model using best_C and best_gamma
best_C = 2^C(idx); best_gamma = 2^gamma(idx); %# ...


This also does a grid lookup ... but using matlab ... not using ... maybe it helps ...



You can use the provided matlab script instead of FAQ

Q: How could I use the MATLAB interface to select parameters?

bestcv = 0;
for log2c = -1:3,
  for log2g = -4:1,
    cmd = ['-v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
    cv = svmtrain(heart_scale_label, heart_scale_inst, cmd);
    if (cv >= bestcv),
      bestcv = cv; bestc = 2^log2c; bestg = 2^log2g;
    fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv);




All Articles