ruwanego
ruwanego

Reputation: 427

How can I perform ten-fold cross validation with LibSVM?

Where can I find an example of using LibSVM (Java/Python) to perform 10-fold cross validation on a data set?

Upvotes: 1

Views: 3520

Answers (2)

aykut
aykut

Reputation: 563

In case of Java;

You can use svm_cross_validation() method which is provided by libsvm.

double[] target = new double[svmProblem.l];
svm.svm_cross_validation(svmProblem, svmParameter, 10, target );

You should have created your svm_problem and svm_parameter instances (svmProblem and svmParameter in the sample). 3rd parameter of the function is the number of folds, target is the array of predicted label values for each sample of the data set. So, length of the target array should be same with the length of your input data.

Upvotes: 5

thkala
thkala

Reputation: 86333

Unless I am missing something in your question, the svmutil.py script in the python subdirectory of the libsvm package offers a n-fold cross validation mode:

-v n: n-fold cross validation mode

The svm_train.java file in the java subdirectory also offers such an option:

+"-v n : n-fold cross validation mode\n"

You can have a look at those two files for some sample code.

Upvotes: 0

Related Questions