problems after addpath libsvm library in matlab
Asked Answered
S

2

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

Saiz answered 22/3, 2013 at 11:17 Comment(3)
Naming conflict with the Bioinformatics svmtrain and the libsvm svmtrain? LIBSVM FAQMariannamarianne
I changes this one CXX = g++ in the Makefile with CXX = g++-X.Y . but still errorSaiz
That's not what I am suggesting. Try using the full path name when you run the libsmv svmtrain.Mariannamarianne
S
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'));
Suction answered 22/3, 2013 at 17:54 Comment(9)
thank you so much for ur help , it's working but i have to install sdk first.Saiz
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??Saiz
@Saiz 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: #9042253Suction
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 youSaiz
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.Suction
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?Saiz
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: hal.archives-ouvertes.fr/docs/00/10/39/55/PDF/… Also please make sure you know 'one-vs-one', 'one-vs-all', 'cross validation' by using google.Suction
@Saiz libsvm can perform multi-class classification. It uses one vs. one methodology. See the FAQ section of libsvm here 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?Crawler
@Parag I remember it uses one-vs-one for multi-class. yes. we use one-against-the rest. (one-vs-all the others).Suction
G
3

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

Grazier answered 6/5, 2013 at 18:27 Comment(1)
this is a far better answer than recompiling libsvmKinser

© 2022 - 2024 — McMap. All rights reserved.