如何使用grid.py进行参数选择? [英] how can i use grid.py for parameter selection?
本文介绍了如何使用grid.py进行参数选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
%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 ...也许有帮助...
i want to select parameters c and gamma for C-SVM classification using the RBF (radial basis function) kernel with libsvm\tools\grid.py, but i dont know how it's possible? i installed libsvm and gnuplot and python and runned the grid.py in python, but it had error and didn't show the results.
解决方案
%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); %# ...
This performs grid search as well... but using matlab... not using grid.py... maybe this helps...
这篇关于如何使用grid.py进行参数选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文