File Exchange

image thumbnail

Multi Class Support Vector Machine

version 1.4 (2.17 KB) by

This function removes out the limitation of MATLAB SVM function of two class and uses more classes.

4.11765
21 Ratings

186 Downloads

Updated

View License

This function can classify more than two classes which is limited in MATLAB SVM. This is primary work and does not include plotting function for SVM.
This is version 3.0 of original function which removes some limitations of first & Second one.

Comments and Ratings (63)

YU FENG

This code have many errors when used for more than two classes?!

Wasiur

Wasiur (view profile)

@Ambika, your GroupTrain would be row matrix not column matrix. Correct version of your code:
T = [ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42]
Test =[ 2 26 ; 4 49; 5 55; 1.9 12];
GroupTrain=[1;1;2;2;3;3;2;2]
results = multisvm(T, GroupTrain', Test);
disp(results);

Results:
     1
     2
     3
     1

SUNNY NEGI

Hello everyone, Can anybody tell me how to resolve this problem....
Error using seqminopt>seqminoptImpl (line 198)
No convergence achieved within maximum number of iterations.

Error in seqminopt (line 81)
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in svmtrain (line 499)
[alpha, bias] = seqminopt(training, groupIndex, ...

Error in multisvm (line 20)
models(k) = svmtrain(TrainingSet,G1vAll);

Error in svmfinal (line 56)
Class = multisvm(TRAINING,Y,X2);

Ambika PS

sir thanks a lot for uploading your code.

I got the answer to my question. Thanks to @MML

Ambika PS

Sir I tried your code for the following experiment:
T = [ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42]
Test =[ 2 26 ; 4 49; 5 55; 1.9 12];
GroupTrain=[1;1;2;2;3;3;2;2]
results = multisvm(TrainingSet, GroupTrain, TestSet);
disp(results);
 I got the result as :
1
1
1
1
where it should have been
1
2
3
1
Is it an error?

bassma bassma

le code donne des résultats erronés

Ayush Pandey

Abdul Latif

Hi,
i have problem with my code. I want to make three class or groups in svm, but this code don't work.
Can anybody help me please?
Thank you very much.

Code :
GLCM2 = graycomatrix(I3,'Offset',[4 4]);
stats = graycoprops(GLCM2,{'contrast','homogeneity','energy','Correlation'})

a = stats.Contrast;
b = stats.Correlation;
c = stats.Energy;
d = stats.Homogeneity;

set(handles.edit1, 'String',a);
set(handles.edit2, 'String',b);
set(handles.edit3, 'String',c);
set(handles.edit4, 'String',d);

load class_uji2.txt;// document text class or group with three class {0,1,2}
load data_uji2.txt;// document training data
uji1=data_uji2(:,1:2:3:4);
massa = svmtrain(uji1,class_uji2)

uji = svmclassify(massa, [a b c d]) // [a b c d] test data with glcm
if uji == 0
hasil = 'Normal'
else if uji == 1
hasil = 'Benign Cancer'
else
hasil = 'Malignant Cancer'
end
end

set(handles.edit5, 'String',hasil);

hello_world

What is the size of input variable 'C' in your code? I am not sure if it is equal to the size of training set, or to the size of raw data set?

How to perform 10 fold cross-validation?

How to use custom kernel?

Sneha Jain

hii...i am doing retrieval of images..i have classified my target image using code of Cody which is a modified version of your code...there are three classes namely, 1,2 and 3..the class of my target image is displayed as 1. now i want to retrieve all images of class 1 and display the images. how to do that??
Urgent

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@can liu
The other submission of this file by user Cody shows the result as
2
3
3
I don't know whether his code is wrong or whether it is systems error.

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@aarini
No idea about plotting multisvm now, will work for plotting in free times.
@KALYAN KUMAR
Read Comments inside file for train and other informations.
@saadoon al-sumaidaee
Thanks to Manu BN to resolve your query.
@mesut maleki
Check for Previous Comments you'll got answer.
@aakaash vaidyanathan
It is working in present version also..if you are unable to use it then you can download its toolbox available with latest Matlab version or download and update from the link above in page.
@can liu
Sorry Can Liu, I have run your code and got correct result. Don't know why your system gave incorrect answer. If here will get same incorrect result then share here. I will look further for problems.

can liu

I think there is an error in your code
 R=[1 2 3 4;1 2 3 4;3 4 5 6;7 8 9 10;7 8 9 10]
          C=[2 2 1 3 3]'
        T=[1 2 3 4;7 8 9 10;3 4 5 6]
       [IT1]=multisvm(R,C',T)

result:IT1 =

     1
     3
     1
the right answer should be
2
3
1
I think the error found in your code

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

when t=1,will happen error .
i am looking forward to your relay
 

in present matlab mutisvm function not working, then how to train and use the svm??
its coming Undefined function or variable 'multisvm'. please help

mesut maleki

hi
i have this error. when features are 480*25. when i have 480*20 it is correctly work!!!
please help me
Error using seqminoptIseqminoptImpl (line 198)
No convergence achieved within maximum number of iterations.

Error in seqminopt (line 81)
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in svmtrain (line 505)
    [alpha, bias] = seqminopt(training, groupIndex, ...

Error in multisvm (line 20)
    models(k) = svmtrain(TrainingSet,G1vAll);

Error in SVM_4tayy (line 233)
[result]=multisvm(f,TR_label,ft);

Manu BN

Manu BN (view profile)

Plz note that the group must strictly be a row vector. For e.g to classify into three classes: (where each class is of 10*13)

T = [Class1;Class2;Class3];
C = [ zeros(1,10) ones(1,10) 2*ones(1,10) ];

The dimensions of T is 30*13
The dimensions of C is 1*30

Guess u got the point !!!

Hi all,
Just I need to know is the Train set dimension is considered or no?
i.e. I have a vector of features, each vector length has equal to 32256, and I have a six class. Can I use multi class in the classifying the facial expressions?

Tung Le

KALYAN KUMAR

what exactly group train is??
can i know train data and test data matrix sizes for example?

aarini

aarini (view profile)

How to plot the classified result? Kindly help.

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@kamil kamil, @ LEON, @chaou, @ Anirban, @ M@lik Ali, @ yavuzca23, @ Niyalatul, @ Mehrdad Azh, @ Subha, @ jamila, @ Aya, @ billel madridi, @ Weizhi Li, @ raha, @ mustafa alnasser, @ jie, @ shila, @ sunil ds, @ Roshni rahul
Sorry Everyone again, for late reply because of very busy schedule I am this much late to reply.
Not able to answer every query, I would suggest everyone to read answers given by many other members in Commenets to solve many your problems before asking.
I think many of you are not finding example of file in Preview Image above. For you I am giving 1 example given by user Cody.I appreciate Cody work.

%% 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.
T=[ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42];
test=[3 34; 1 14; 2.2 25; 6.2 63];
C=[1;1;2;2;3;3;2;2];
results = multisvm(T, C, test);
disp('multi class problem');
disp(results);

Thank You for taking help.

Roshni rahul

sir,im doing my thesis on facial expression recognition.i am using CK+ dataset.can use please tell me how to create training set,test set and group set as i want to classify expression into 6 classes ie anger,surprise,sad,disgust,happiness and Fear.

sunil

sunil (view profile)

sir, pl inform me how to find input values to the training set, test test and group test?

sunil

sunil (view profile)

sir, i'm new to matlab. how to find values to give test set and training set.

shila

shila (view profile)

Hello sir. i have used this function. i have to classify 3 classes. but its not getting expected result.. its shows only one class its unable to group rest two classes.. whts the problme?

jie

jie (view profile)

Could you please tell me the reference of your aglorithm? Thanks a lot.

I am using neural network for classification of four classes and I am trying to use SVM to do the same analysis but I do not how to reflect 4 input vectors on the three sets (group, test.)

raha

raha (view profile)

hi
i am new in svm
i wonder how calculate accuracy in this algorithm
plz help me

Weizhi Li

Error using seqminopt>seqminoptImpl (line 198)
No convergence achieved within maximum number of iterations.

Error in seqminopt (line 81)
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in svmtrain (line 499)
    [alpha, bias] = seqminopt(training, groupIndex, ...

Error in multisvm (line 20)
    models(k) = svmtrain(TrainingSet,G1vAll);

I think it does not work

hey ; why i can't see the file could you send it to me to( billelnet009@gmail.com )
please i really need it it's emergency

Aya

Aya (view profile)

please i need to know it's one vs all or one vs one relation??

jamila

jamila (view profile)

Hi
thank you for this code, it great ; but i have an error when i try to change the kernel function. The error is " Error using ==> seqminopt>seqminoptImpl at 203
No convergence achieved within maximum number of iterations.

Error in ==> seqminopt at 84
[alphas offset] = seqminoptImpl(data, targetLabels, ...

Error in ==> svmtrain at 541
    [alpha bias] = seqminopt(training, groupIndex, ...

Error in ==> multisvm at 49
            svmStruct = svmtrain(T,newClass,'kernel_function','linear' ); %
            I am using rbf kernel function, you must change it also

Error in ==> teststa at 61
[itrfin] = multisvm( data,classa,test )"

can you help me please?

Subha

Subha (view profile)

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..

Mehrdad Azh

Hi

Could you please upload a test case for us to see how to use it?

Thnx.

Mehrdad Azh

Hi

Could you please upload a test case for us to see how to use it?

Thnx.

Niyalatul

Hi,
I'm still new to learn this topic.
How can I learn multiclass svm more specific (literature suggestions that I can use, like the books and other)?

Thanks

Cody

Cody (view profile)

  • 1 file
  • 211 downloads
  • 3.63636

Please see my revision of this code here:
http://www.mathworks.com/matlabcentral/fileexchange/39352

Cody

Cody (view profile)

  • 1 file
  • 211 downloads
  • 3.63636

Hello, thanks for your contribution. It helped me out, but it was confusing and I have re-written and uploaded a clearer function that does the same thing. It is only about 15 lines of code. I'll provide a link when it is approved.

Cody

Cody (view profile)

  • 1 file
  • 211 downloads
  • 3.63636

yavuzca23

Hi,
does this code work with 6 inputs which include 3200 samples , and 30 classes(outputs)?

M@lik Ali

Can any one tel me how i can run this program, please i am new in mat-lab.
what and how to give input to the function,
i have a feature vector of the images how i can use it.

Anirban

Hey Anand

Thank for sharing 'multisvm' code, its really useful! But can you tell is there any way to get the learned models so that while testing for next time I dot need to train again. Because training in every test step is pretty time consuming.

Thanks in advance for your suggestion

MML

MML (view profile)

thanks a lot Anand for sharing your code.
I found it very useful.

@Chaou and @Yap
The class-vector (C) should be a row vector. I had the same problem when I tried to use a column vector for C. I hope this resolves the problem.

Yap

Yap (view profile)

me too, my result output all 1 1 1 1.....I hope some1 could help!

Chaou

Chaou (view profile)

Hello, I'd like first to thank you for this program. But unfortunately, it doesn't work in my case.
I'm obtaining a itrfin=1 in all cases. I'm using a test vector of 30 elements, and I obtain a itrfin vector = 1 1 1 1.... 1.

Why ? I really appreciate your help. Thanks !

Shikha

Shikha (view profile)

LEONG

LEONG (view profile)

Hello,
I am searching a way of plotting the multi-svm.

Description about my project:
My application is actually is to classify 4 four classes of different input signals. i have three trial for each subject which each of them need to four different of movement..

Here is my some questions:

1. Does multi-svm that Anand provided able to plot the graph?
2. If yes, how to plot it ..i mean the algorithm..
3. beside, i want to know whether the multi-svm support for classperf ? because i test it before.. it come out with error as:

??? Error using ==> classperf at
270
The classifier output CLASSOUT
does not have the same size
as the ground truth and there was
not any TESTIDX provided.

Error in ==>
multisvm_classification2 at 63
classperf(cp,classes);

4. What does that mean..i got find the error at internet but the the problem still did'nt solve..
any solution for it ?

5. Can i use more than two training data but with one test in multi-svm version 2.0 or 3.0 ?can i use it to obtain classperformance of multi-training and test for many data?

Your suggestion will be appreciated..
thank you

kamil kamil

Hello,
Thank you for this program.
How we can determine the best (Sigma, C) with this method ?

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

Download Version 3.0 of this function and give suggestions for improving it. Version 3.0 includes the usage of multiple row testing matrix input.

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@pallavi
Sorry for late reply, but I was little bit busy.
You can use '|' instead of '||' or '&' instead of '&&' in function,hope this will work for your application. You can refer Operators and Flow Control in Matlab for more information.
Thank You for taking help.

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@Gomathi C,
Sorry for late reply, but I was little bit busy.
By the way you don't have to worry about using this function for two class SVM. You can not use this function for two classes, because their is an inbuilt MATLAB function for two classes classification present in MATLAB IDE. I created this function to extend the facilities of inbuilt MATLAB function.
I am not able to understand your output of GLCM but you can look image associated with my file in MATLAB file exchange for example of using my SVM function.
If you still find any problem in using your inputs for your program then please clarify me whole parameters you are using for GLCM like autoc, contr etc. So that I am able to tell you more clearly.
Hope this will help you.
Thanks for using my function.

Gomathi C

I actually obtained the texture features using GLCM. My output for that was

stats =

    autoc: [1.857855266614132e+000 1.857955341199538e+000]
    contr: [5.103143332457753e-002 5.030548650257343e-002]
    corrm: [9.512661919561399e-001 9.519459060378332e-001]
    corrp: [9.512661919561385e-001 9.519459060378338e-001]
    cprom: [7.885631654779597e+001 7.905268525471267e+001]

Now how should I give this as an input to the SVM program. Kindly guide me.

pallavi

when I run the program , I fet an error:
??? Operands to the || and && operators must be convertible to logical scalar values.
how to solve it?

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@guo,
Hi, you correctly spotted the algorithm behind this file.
Now take an example,
Suppose
T=[1 2 3 4;2 3 4 5;3 4 5 6;4 5 6 7]
C=[1 2 3 4]
tst=[3 4 5 6]
then run the function and then debug it step by step to understand algorithm.
It becomes clearer how the class is calculated.
Thanks for your interest.

guo

guo (view profile)

Hi again. I wonder how you solve an multi-class problem. It is likely that you decompose an M-class problem into a series of two-class problems using one-against-all implementation, right?

guo

guo (view profile)

Hello!Could you please give a detailed example for input parameter specification?

Anand Mishra

Anand Mishra (view profile)

  • 1 file
  • 186 downloads
  • 4.11765

@Aziz,
Dear Aziz, svmclassify never makes 'classes' variable as a row vector of 0 and 1. The 'classes' can used only for checking loop condition i.e.,whether loop is to be terminate or not (check in while condition).
Variables 'newClass' and 'c1' can takes form of vector(0 and 1) and changing their sizes in each iteration.

I am using operands || and && because I am working on C Programming also so I am habitual of using this instead of logical scalar values.

May this solve out your confusions. Read Comments on file thoroughly. Thanks for taking help.
Regards...Anand Mishra.

Aziz

Aziz (view profile)

This is great but I wonder what purpose the 'classes' variable serve? At first it looks like an indicator to keep looping if the 'classes' variable still not equal 1 (line 32, line 65-67) but then you use it as result from svmclassify (line 39), which will make it a row vector of 0 and 1. Hence it couldn't be used in loop indicators. It would make this appear

??? Operands to the || and && operators must be convertible to logical scalar values.

Am I missing something?

Updates

1.4

Now the function is available in toolbox also.

1.3

This is version 3.0 of original function.

1.2

This is the update version 3.0 of original created Multi-Class SVM function by me.

1.1

This is Version 2.0 of Original Function.

MATLAB Release
MATLAB 7.12 (R2011a)
Acknowledgements

Inspired: Multi Class SVM

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video