我想为C-SVM分类选择参数c和gamma 使用libsvm \ tools \ grid.py的RBF(径向基函数)内核,但我不知道它是如何可能的?我安装了libsvm和gnuplot以及python并在python中运行了grid.py,但它有错误并且没有显示结果。
答案 0 :(得分:12)
%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));
end
%# 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); %# ...
这也执行网格搜索......但是使用matlab ...不使用grid.py ...这可能有帮助......
答案 1 :(得分:6)
您可以使用提供的matlab脚本而不是grid.py FAQ
问:我如何使用MATLAB接口进行参数选择? http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f803
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;
end
fprintf('%g %g %g (best c=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv);
end
end