Code covered by the BSD License  

Highlights from
Multi Class SVM

3.72727

3.7 | 12 ratings Rate this file 301 Downloads (last 30 days) File Size: 1.5 KB File ID: #39352

Multi Class SVM

by

 

Model and classify training/test data sets into more than 2 classes with SVM.

| Watch this File

File Information
Description

Support Vector Machines only classify data into two classes. This function removes that restriction by "searching" for the correct class for each row in the test data set.

This code is a clarification and optimization of Anand Mishra's code found here:
http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine

Use only with more than 2 classes, otherwise use svmtrain() directly.

Usage Example:

%% SVM Multiclass Example
% SVM is inherently one vs one classification.
% This is an example of how to implement multiclassification using the
% one vs all approach.
TrainingSet=[ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42];
TestSet=[3 34; 1 14; 2.2 25; 6.2 63];
GroupTrain=[1;1;2;2;3;3;2;2];
results = multisvm(TrainingSet, GroupTrain, TestSet);
disp('multi class problem');
disp(results);

Acknowledgements

Multi Class Support Vector Machine inspired this file.

Required Products Bioinformatics Toolbox
Statistics Toolbox
MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (30)
11 Jul 2014 aabiya

please let me know how to calculate the group train matrix. I have calculated the train and the test set but can't understand group train variable.

27 Jun 2014 Amarjot

Thanks for the code!

26 Jun 2014 Peter

Thanks for the code. With the latest version of MATLAB (2014a) you don't need the Bioinformatics Toolbox to run the code.

29 May 2014 ashok

sir, i think the code will not check for the next class predicted, please check if it is 5 class

23 Apr 2014 Borja Heriz

Thank you so much Cody. It has been really helpfull for my current project!!!!!

22 Apr 2014 shah

Please cody help me how to put matrix of my image data set in training set,test set and group set ?II am new so no idea!

I do not undersand how to do these steps for my data set
TrainingSet=[ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42];
TestSet=[3 34; 1 14; 2.2 25; 6.2 63];
GroupTrain=[1;1;2;2;3;3;2;2];

11 Apr 2014 Hamid Ramezani

It seems that the code considered each set of data individually, and not jointly. Do you have any reference for your algorithm? If so, I would be thankful if you share that.

11 Feb 2014 Walid

Hi, and thanks for the code. Although I think there is a minor mistake in the code. In the last line, before the "end", the line "result(j) = k;" should be "result(j) = u(k);".

21 Jan 2014 Javed  
05 Nov 2013 mahdi

Hi.Thanks for your implementation but I think it is incorrect. When you return 0 or 1 from svmclassify, you maybe have several zeros or ones with different classes!!!
I think you need to compute the real values(before execute sign function) and assign the test data to the class which has the biggest positive ouput.

25 Oct 2013 Lulin

This code can be improved if it can determine a class membership when a test observation belongs to multiple classes.

21 Oct 2013 raji

thanks for the updated contents!!! very much helpful...

17 Jun 2013 joy barbosa

thank you so much for sharing your code sir Cody. It saved my day!!! 2thumbs up!

17 Jun 2013 joy barbosa  
13 Jun 2013 Matthew Klingensmith  
10 Apr 2013 B mohan

I am doing project on facial expression recognition using LBP(Local Binary pattern). i.e finding expressions whetherit is fear, anger, surprise, disguist, happy, joy. First I loaded jaffee database. Then applied preprocessing on it. And the features are extracted using LBP(Local Binary pattern). I don't know how to use multiclass SVM(Support vector Machine). Can this code be useful to my project. What shoud I take input TrainingSet ,GroupTrain,TestSet. Please help me.

10 Apr 2013 B mohan

I am doing project on facial expression recognition using LBP(Local Binary pattern). i.e finding expressions whetherit is fear, anger, surprise, disguist, happy, joy. First I loaded jaffee database. Then applied preprocessing on it. And the features are extracted using LBP(Local Binary pattern). I don't know how to use multiclass SVM(Support vector Machine). Can this code be useful to my project. What shoud I take input TrainingSet ,GroupTrain,TestSet. Please help me.

08 Apr 2013 Nimmi

Can this be used in model predictive control algorithm.

14 Mar 2013 Subha

Sir is it possible for us to add weights for the feature with this code??? if so how to proceed with it..?

16 Feb 2013 Subha

Thank you sir.. now trying out with best possible combination...

11 Feb 2013 Cody

If you are getting all 1s, you should try using different options for the svmtrain function. for example:
options = statset('MaxIter',2000000);
models(k) = svmtrain(X_Train,G1vAll,'options',options,...
'kernel_function','rbf','rbf_sigma',16);

02 Feb 2013 Subha

sir,
is it possible to plot confusion matrix for this program... if so how to do tat.. we have tried many combinations but does not working...
....
else any method to calculate the performance of the overall system from test data's..

27 Jan 2013 Subha

oh.. ok sir.. now its working.. thank a lot...

23 Jan 2013 Cody

Ok, you are not using the function correctly.

load your .mat file, then pass your new variables as parameters to the function.

22 Jan 2013 Subha

Sir,
I've loaded data and worked in 2 class problem. its works well...
this is my code.. i've just loaded the data and didnt change anything more.
....
function [result] = multisvm(TrainingSet,GroupTrain,TestSet)
load TrainingSet
load GroupTrain
load TestSet
u=unique(GroupTrain);
numClasses=length(u);
result = zeros(length(TestSet(:,1)),1);

%build models
for k=1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G1vAll=(GroupTrain==u(k))
models(k) = svmtrain(TrainingSet,G1vAll,'kernel_function','rbf');
end

%classify test cases
for j=1:size(TestSet,1)
for k=1:numClasses
if(svmclassify(models(k),TestSet(j,:)))
break;
end
end
result(j) = k;
end

where,
training set is 16*4
test set is 4*4
grouptrain is 16*1
I'm using Matlab (R2010a)

21 Jan 2013 Cody

It sounds like you need to learn how to load .mat files. Simply type 'load xxx.mat' where xxx is the file name. You would then need to sample your data into training, test, and label sets for use with this function.

Can you show the code you are using?

21 Jan 2013 Subha

Hello Sir,
Thanks for the code.. it works well when we give data... but i'm working with large set of data, where i need to load it(.mat)..
I cant load data, if i do so then it showing error message
??? Undefined function or variable 'GroupTrain'.

Error in ==> neuro_multisuport at 40
u=unique(GroupTrain);
where grouptrain is a column vector(16*1) in .mat format
.. can u please suggest me how to modify the code when i load data...

21 Dec 2012 Niyalatul

Hello,
Thanks for your program. I am still new to learn this topic. This is very helpful

21 Dec 2012 Niyalatul  
21 Dec 2012 Niyalatul  

Contact us