How to optimize the parameters using libsvm?
3 次查看(过去 30 天)
显示 更早的评论
Libsvm FAQ suggests this piece of code:
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
Which works perfectly fine as long as crossvalidation is included. However, I need to do the crossvalidation manually, because I need to artificially create data for the undersampled class I only want to have in the training set. But if I remove the crossvalidation, then the result is a struct which cannot be compared in the if statement. Does anybody know how to manipulate the code to make it work or how to do it differently?
0 个评论
采纳的回答
Viren Gupta
2018-8-2
svmtrain function returns a SVMStruct in MATLAB. SVMStruct is the trained svm model. You can use svmclassify to test your model on the test data and then calculate accuracy from the predicted test labels. The accuracy calculated can then be compared with the bestcv(as done in the if condition) to cross validate.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!