11

I'm working with libsvm and I must implement the classification for multiclasses with one versus all.

How can I do it?
Does libsvm version 2011 use this?


I think that my question is not very clear. if libsvm don't use automatically one versus all,I will use one svm for every class, else how can i defined this parameters in the svmtrain function. I had read README of libsvm.

Amro
  • 123,847
  • 25
  • 243
  • 454
images
  • 171
  • 1
  • 1
  • 6
  • Explain us what you've tried? If you have not tried anything yet, read the README. ps: svmlib or libsvm? – Oli Jan 28 '12 at 12:12
  • 1
    related question: http://stackoverflow.com/questions/4976539/support-vector-machines-in-matlab – Amro Jan 29 '12 at 00:21
  • k(k-1)/2 classifiers are formed. I don't think k(k/1)=2 are formed. Just for information. Besides that, Amro's answer is perfect... – lakshmen Feb 01 '12 at 07:22

1 Answers1

36

According to the official libsvm documentation (Section 7):

LIBSVM implements the "one-against-one" approach for multi-class classification. If k is the number of classes, then k(k-1)/2 classifiers are constructed and each one trains data from two classes.

In classification we use a voting strategy: each binary classification is considered to be a voting where votes can be cast for all data points x - in the end a point is designated to be in a class with the maximum number of votes.

In the one-against-all approach, we build as many binary classifiers as there are classes, each trained to separate one class from the rest. To predict a new instance, we choose the classifier with the largest decision function value.


As I mentioned before, the idea is to train k SVM models each one separating one class from the rest. Once we have those binary classifiers, we use the probability outputs (the -b 1 option) to predict new instances by picking the class with the highest probability.

Consider the following example:

%# Fisher Iris dataset
load fisheriris
[~,~,labels] = unique(species);   %# labels: 1/2/3
data = zscore(meas);              %# scale features
numInst = size(data,1);
numLabels = max(labels);

%# split training/testing
idx = randperm(numInst);
numTrain = 100; numTest = numInst - numTrain;
trainData = data(idx(1:numTrain),:);  testData = data(idx(numTrain+1:end),:);
trainLabel = labels(idx(1:numTrain)); testLabel = labels(idx(numTrain+1:end));

Here is my implementation for the one-against-all approach for multi-class SVM:

%# train one-against-all models
model = cell(numLabels,1);
for k=1:numLabels
    model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
end

%# get probability estimates of test instances using each model
prob = zeros(numTest,numLabels);
for k=1:numLabels
    [~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
    prob(:,k) = p(:,model{k}.Label==1);    %# probability of class==k
end

%# predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel)    %# accuracy
C = confusionmat(testLabel, pred)                   %# confusion matrix
Amro
  • 123,847
  • 25
  • 243
  • 454
  • 2
    can you provide me an example for one against all with libsvm? – images Jan 30 '12 at 12:56
  • I try to use the code provided by lakesh in his questions(Multi-Class SVM( one versus all), is it correct? – images Jan 30 '12 at 18:08
  • @images: I've added a sample implementation – Amro Jan 31 '12 at 19:56
  • '@ Amro :Thank you very much for your efforts.' – images Feb 01 '12 at 19:54
  • When I copy your code I get the following error at the line `[~,~,labels] = unique(species);` in matlab: `Expression or statement is incorrect--possibly unbalanced (, {, or [.`. Could you help me please? – Zahra E Dec 23 '12 at 17:17
  • @Ezati: the `~` syntax requires R2009b. Use a dummy variable instead if you are using an older version of MATLAB – Amro Dec 23 '12 at 18:36
  • Thanks a lot, I replaced it with `[dummy,dummy,labels] = unique(species);` and it worked fine – Zahra E Dec 24 '12 at 14:40
  • May I ask you another question: http://stackoverflow.com/q/14024740/1071703 ...if you have time of course :) – Zahra E Dec 25 '12 at 09:46
  • @Ezati: I have posted an answer there. – Amro Dec 26 '12 at 14:44
  • Again thank you for your great response :) – Zahra E Dec 26 '12 at 16:39
  • Can you expliain what these parameters stand for '-c 1 -g 0.2' – MVTC Dec 07 '14 at 23:50
  • 2
    @MVTC: you should probably read the *libsvm* guide first; `c` is the penalty parameter of the error term in C-SVC, `g` is the RBF kernel gamma parameter. One usually use cross-validation to find the best values for these parameters, see here for an example: http://stackoverflow.com/a/9049225/97160 – Amro Dec 08 '14 at 01:23
  • How would I do this for a dataset size of 50k samples and of dimensionality 4000? Matlab seems to be taking too long. *I added the -t 0 option for a linear kernel – Arturo Feb 12 '16 at 23:33
  • What about one class SVM? How do you define the Labels vector in that case? Thank You. – Royi Mar 23 '17 at 20:32
  • @Amro: when I use your example, I reach to this error: `Invalid MEX-file '...\libsvm-3.22\libsvm-3.22\matlab\svmtrain.mexw64': The specified module could not be found..` – Amin Jul 14 '17 at 11:47
  • 1
    @Amin: It sounds like a build problem, see this post for instructions on how to compile libsvm for MATLAB: https://stackoverflow.com/a/15559516/97160. If you are still having problems, consider using *Dependency Walker* to troubleshoot: https://www.mathworks.com/matlabcentral/answers/92362-how-do-i-determine-which-libraries-my-mex-file-or-stand-alone-application-requires – Amro Jul 14 '17 at 12:34
  • @Amro: perfect. – Amin Jul 14 '17 at 13:17
  • @Amin: as was explained in the above post, one-vs-one is the approach implemented in libsvm, just call `svmtrain` directly with multi-class labels... Here is another answer of mine that compares the two: https://stackoverflow.com/a/14042056/97160 – Amro Jul 14 '17 at 15:58
  • @Amro: for your reference: https://www.mathworks.com/matlabcentral/answers/348834-why-does-this-confusion-matrix-not-a-logical-one – Amin Jul 15 '17 at 08:13
  • 1
    @Amin: Don't expect full answers in comments, but you can't just blindly apply machine learning algorithms, and expect good results. You should read up on SVM and its parameters (kernels, C, gamma, etc.), and how you would need to do a [grid search](https://stackoverflow.com/a/9049225/97160) to find good values using *cross-validation*. You should also look into [preprocessing](https://stackoverflow.com/a/7863715/97160) the data (normalizing the features at the least)... Good luck. – Amro Jul 15 '17 at 12:58
  • @Amro Please can you help me here? thank you a lot https://stackoverflow.com/questions/65449934/multi-class-svm-one-vs-one-always-giving-the-same-label – Christina Dec 25 '20 at 17:05