How to optimize the parameters using libsvm?

조회 수: 3 (최근 30일)
Stef
Stef 2018년 7월 18일
답변: Viren Gupta 2018년 8월 2일
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에서 Agriculture에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by