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?

采纳的回答

Viren Gupta
Viren Gupta 2018-8-2
Firstly MATLAB does not support svmtrain with cmd parameter as suggested here.
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 个)

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by