problems after addpath libsvm library in matlab

user2157806 picture user2157806 · Mar 22, 2013 · Viewed 7.4k times · Source

i want to know how libsvm works. I tried this code in this link [1]: 10 fold cross-validation in one-against-all SVM (using LibSVM) . It's working (I havent added path libsvm library in matlab) but after i add libsvm library. it is not working. I have no idea how to solve it. there's an error :

Error using svmtrain (line 233)
Y must be a vector or a character array.

Error in libsvmtrain_ova (line 11)
        models{k} = svmtrain(double(y==labels(k)), X, strcat(opts,' -b 1 -q'));

Error in libsvmcrossval_ova (line 10)
        mdl = libsvmtrain_ova(y(trainIdx), X(trainIdx,:), opts);

Error in main (line 9)
acc = libsvmcrossval_ova(labels, data, opts, nfold);

does anyone help me how to solve it?? thank you

Answer

tqjustc picture tqjustc · Mar 22, 2013

I followed the post you referred to and I got the results without errors. For me, the cross validation accuracy for 'fisheriris' dataset is 96.6667%. For you, I think the error is that the error is from 'svmtrain' just as the first comment said. In the following, I will show how I ran the code.

1) download the libsvm from http://www.csie.ntu.edu.tw/~cjlin/libsvm/ and unzip it.

2) change the names of files svmtrain.c and svmpredict.c in \libsvm-3.16\matlab\ to be libsvmtrain.c and libsvmpredict.c. And then locate make.m in the same folder and change line 16 and line 17 to be

mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmtrain.c ../svm.cpp svm_model_matlab.c
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmpredict.c ../svm.cpp svm_model_matlab.c

3) run make.m you just changed to mex *.c files.

4) following the accepted answer of the post 10 fold cross-validation in one-against-all SVM (using LibSVM) , you create four .m files for each function, crossvalidation.m , libsvmcrossval_ova.m, libsvmpredict_ova.m, libsvmtrain_ova.m and run the main function provided by that answerer, which is as follows:

clear;clc;
%# laod dataset
S = load('fisheriris');
data = zscore(S.meas);
labels = grp2idx(S.species);

%# cross-validate using one-vs-all approach
opts = '-s 0 -t 2 -c 1 -g 0.25';    %# libsvm training options
nfold = 10;
acc = libsvmcrossval_ova(labels, data, opts, nfold);
fprintf('Cross Validation Accuracy = %.4f%%\n', 100*mean(acc));

%# compute final model over the entire dataset
mdl = libsvmtrain_ova(labels, data, opts);



acc = libsvmtrain(labels, data, sprintf('%s -v %d -q',opts,nfold));
model = libsvmtrain(labels, data, strcat(opts,' -q'));