6

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

Community
  • 1
  • 1
user2157806
  • 319
  • 2
  • 8
  • 17
  • 4
    Naming conflict with the Bioinformatics `svmtrain` and the libsvm `svmtrain`? [LIBSVM FAQ](http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#/Q9:_MATLAB_interface) – AGS Mar 22 '13 at 11:31
  • I changes this one CXX = g++ in the Makefile with CXX = g++-X.Y . but still error – user2157806 Mar 22 '13 at 11:39
  • That's not what I am suggesting. Try using the full path name when you run the libsmv `svmtrain`. – AGS Mar 22 '13 at 11:45

2 Answers2

8

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'));
Community
  • 1
  • 1
tqjustc
  • 3,624
  • 6
  • 27
  • 42
  • thank you so much for ur help , it's working but i have to install sdk first. – user2157806 Mar 22 '13 at 19:22
  • anyway .. do u know the different between acc = libsvmcrossval_ova(labels, data, opts, nfold); fprintf('Cross Validation Accuracy = %.4f%%\n', 100*mean(acc)); and this one : acc = libsvmtrain(labels, data, sprintf('%s -v %d -q',opts,nfold)); why the result is different?? – user2157806 Mar 22 '13 at 19:26
  • 1
    @user2157806 To solve multi-class classification problem, there are many approaches for example one-vs-all, and one-vs-one. In libsvmcrossval_ova, it uses one-vs-all. In libsvmcrossval_ova, it uses libsvmtrain where libsvmtrain is used as a binary classification. But in acc = libsvmtrain(labels, data, sprintf('%s -v %d -q',opts,nfold)), libsvmtrain is used as a multi-class classifier. They use different approaches to solve multi-class problem so the results are different. you can check this post: http://stackoverflow.com/questions/9041753/multi-class-classification-in-libsvm – tqjustc Mar 22 '13 at 19:44
  • so , they are the same function, that is to find the accuracy but the different method , right? so in here( i try 2 class), in my result libsvmtrain get the best results than libsvmcrossval_ova. So, Can i use both of function( libsvmtrain and libsvmcrossval_ova) to classfify two class? because i just need to classify cancer and no cancer. thank you – user2157806 Mar 22 '13 at 19:53
  • there is only one libsvmtrain function. For your case, you can use libsvmtrain directly. since it is binary classification, one-vs-one, one-vs-all are just same. Meanwhile, for any classification problem, you also can use cross validation which can make the model more robust. you'd better google cross validation. I don't think you understand it. 'cross-validation' has no relation with 'one-vs-one' or 'one-vs-all' . They are two different, almost independent concepts. – tqjustc Mar 22 '13 at 20:09
  • ok . one-vs-one' or 'one-vs-all used to solve multi-class classfication problems, but is it possible to use one-against-all models for two class problem? – user2157806 Mar 22 '13 at 20:46
  • 1
    one-vs-all means that you choose one class as positive class and the others as negative class so you can convert multi-class problem to binary classification. So please check what is 'one-vs-one' and what is one-vs-all. read this paper: http://hal.archives-ouvertes.fr/docs/00/10/39/55/PDF/cr102875872670.pdf Also please make sure you know 'one-vs-one', 'one-vs-all', 'cross validation' by using google. – tqjustc Mar 22 '13 at 22:20
  • 1
    @user2157806 libsvm can perform multi-class classification. It uses one vs. one methodology. See the FAQ section of libsvm [here](http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f419) and search for the following question: What method does libsvm use for multi-class SVM ? Why don't you use the "1-against-the rest" method? – Autonomous Mar 23 '13 at 11:24
  • @Parag I remember it uses one-vs-one for multi-class. yes. we use one-against-the rest. (one-vs-all the others). – tqjustc Mar 24 '13 at 21:25
3

There is a very simple way. Set libsvm folder as the priority path in the Set Path Button in your matlab.

Melodie
  • 31
  • 1