support vector machines in matlab
Asked Answered
T

2

11

Could you give an example of classification of 4 classes using Support Vector Machines (SVM) in matlab something like:

atribute_1  atribute_2 atribute_3 atribute_4 class
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3
Traver answered 12/2, 2011 at 5:33 Comment(0)
C
15

MATLAB does not support multiclass SVM at the moment. You could use svmtrain (2-classes) to achieve this, but it would be much easier to use a standard SVM package.

I have used LIBSVM and can confirm that it's very easy to use.


%%# Your data
D = [
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3];
%%# For clarity
Attributes = D(:,1:4);
Classes = D(:,5);
train = [1 3 5 6];
test = [2 4];

%%# Train
model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2');

%%# Test
[predict_label, accuracy, prob_estimates] = svmpredict(Classes(test), Attributes(test,:), model);
Clouet answered 12/2, 2011 at 5:45 Comment(5)
any example to classify the example above?Traver
If I execute model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2'); I get: ??? Error using ==> svmtrain at 172 Group must be a vector.Traver
@darkcminor: Did you copy-paste the all the code I provided? It works for me.Clouet
how did you rectify the Group must be a vector error message? @cMinorHot
@Hot In case you are still looking for a solution: #15620084Tricornered
G
26

SVMs were originally designed for binary classification. They have then been extended to handle multi-class problems. The idea is to decompose the problem into many binary-class problems and then combine them to obtain the prediction.

One approach called one-against-all, builds 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.

Another approach called one-against-one (which I believe is used in LibSVM), builds k(k-1)/2 binary classifiers, trained to separate each pair of classes against each other, and uses a majority voting scheme (max-win strategy) to determine the output prediction.

There are also other approaches such as using Error Correcting Output Code (ECOC) to build many somewhat-redundant binary-classifiers, and use this redundancy to obtain more robust classifications (uses the same idea as Hamming codes).

Example (one-against-one):

%# load dataset
load fisheriris
[g gn] = grp2idx(species);                      %# nominal class to numeric

%# split training/testing sets
[trainIdx testIdx] = crossvalind('HoldOut', species, 1/3);

pairwise = nchoosek(1:length(gn),2);            %# 1-vs-1 pairwise models
svmModel = cell(size(pairwise,1),1);            %# store binary-classifers
predTest = zeros(sum(testIdx),numel(svmModel)); %# store binary predictions

%# classify using one-against-one approach, SVM with 3rd degree poly kernel
for k=1:numel(svmModel)
    %# get only training instances belonging to this pair
    idx = trainIdx & any( bsxfun(@eq, g, pairwise(k,:)) , 2 );

    %# train
    svmModel{k} = svmtrain(meas(idx,:), g(idx), ...
        'BoxConstraint',2e-1, 'Kernel_Function','polynomial', 'Polyorder',3);

    %# test
    predTest(:,k) = svmclassify(svmModel{k}, meas(testIdx,:));
end
pred = mode(predTest,2);   %# voting: clasify as the class receiving most votes

%# performance
cmat = confusionmat(g(testIdx),pred);
acc = 100*sum(diag(cmat))./sum(cmat(:));
fprintf('SVM (1-against-1):\naccuracy = %.2f%%\n', acc);
fprintf('Confusion Matrix:\n'), disp(cmat)

Here is a sample output:

SVM (1-against-1):
accuracy = 93.75%
Confusion Matrix:
    16     0     0
     0    14     2
     0     1    15
Gautea answered 12/2, 2011 at 19:17 Comment(4)
@Gautea is it correct to take average accuracy of k folds if I am using k folds in cvpartition?Acne
@sum2000: yes you report the average accuracy over k-folds, then return the model learned from the entire training dataGautea
But, the accuracy is different everytime I run the code, this is not the case when I calculate error using inbuilt function of cvpartition.Acne
@sum2000: I guess that's because the data is partitioned differently every time you run your code. If you want to reproduce the results, consider setting a seed for the random number generator (see rng function)Gautea
C
15

MATLAB does not support multiclass SVM at the moment. You could use svmtrain (2-classes) to achieve this, but it would be much easier to use a standard SVM package.

I have used LIBSVM and can confirm that it's very easy to use.


%%# Your data
D = [
1           2          3           4             0
1           2          3           5             0
0           2          6           4             1
0           3          3           8             1
7           2          6           4             2
9           1          7           10            3];
%%# For clarity
Attributes = D(:,1:4);
Classes = D(:,5);
train = [1 3 5 6];
test = [2 4];

%%# Train
model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2');

%%# Test
[predict_label, accuracy, prob_estimates] = svmpredict(Classes(test), Attributes(test,:), model);
Clouet answered 12/2, 2011 at 5:45 Comment(5)
any example to classify the example above?Traver
If I execute model = svmtrain(Classes(train),Attributes(train,:),'-s 0 -t 2'); I get: ??? Error using ==> svmtrain at 172 Group must be a vector.Traver
@darkcminor: Did you copy-paste the all the code I provided? It works for me.Clouet
how did you rectify the Group must be a vector error message? @cMinorHot
@Hot In case you are still looking for a solution: #15620084Tricornered

© 2022 - 2024 — McMap. All rights reserved.