Let's say you are trying to classify types of fruit. The training set has multiple fruit examples in it. So setting 1=Apple, 2=Orange, 3=Banana, you would have the following training set labels with two examples per fruit/class:
labels = [1;1;2;2;3;3];
Then you need to select features that can distinguish between your classes. For this fruit example, you might use mass and number of calories as features. So if apples are on average 112g and have 53kcals, oranges are 160g and have 99kcal, and bananas are on average 102g and have 97kcal, your training set features might be:
features = ...
[110,50; %apple example 1
120,60; %apple example 2
150,95; %orange example 1
165,105; %orange example 2
110,105; %banana example 1
98,95]; %banana example 2
So even though apple 1 and banana 1 have the same mass (110), they have very different caloric values and they can be distinguished. Likewise, even though orange 1 and banana 2 have the same number of calories (95), they have very different masses and can be distinguished. These are the relationships that the SVM will learn and allow it to make classifications. Then when you get a new example of a fruit with X mass and Y calories, you can figure out what it is.
in general to use binary classifier for multi-label classification, use the one against all training. It is simple. For each label from all the k labels, label your label j as one and all others as -1. you will obtain k different svms that you can you to classify each label
Start with the linked guide on binary classification. From there, the only change you need to make is to include more than two numerical values in the label vector.
%example binary C-SVM with RBF kernel and two features
labels = [1;1;1;1;-1;-1;-1;-1];
features = [10,20;20,30;10,21;11,25;-10,20;-20,30;-10,21;-11,25];
model = svmtrain(labels,features,'-s 0 -t 2');
%example multiclass C-SVM with RBF kernel and two features
labels = [1;0;1;2;1;2;0;2;0];
features = [11,12;1,2;15,14;27,29;10,9;23,24;2,4;22,24;6,5];
Thank you.Can you please explain how or on what basis have you selected these numerical values e.g. labels=[],features[] etc so that I adapt it for my image dataset?
The numerical values for labels and features above are just examples; I just made them up. However, here is an example of how you would pick them for real data:
Each row of the matrices corresponds to one training example. If you are trying to classify images, then each row is a single image in the training set.
The labels vector corresponds to the "true" classes for each example. If you are trying to classify smiles in facial expression images, then a value of -1 might represent the absence of a smile in that image and a value of 1 might represent the presence of a smile in that image. In the binary example of labels=[1;1;1;1;-1;-1;-1;-1], the training set has four smile images and four non-smile images in that order.
The features matrix corresponds to information about each example that will be used to classify it. If you are trying to classify facial expression images, then information about the shape or texture of the face might be used. Each column in the matrix represents a single feature or dimension. For example, the first example in the above binary example is [10,20]. This might mean that the mouth corners has moved 10 pixels vertically and 20 pixels horizontally. Basically, you need to figure out what the appropriate features would be for your classification task and extract them for each example.
The numbers in the svmtrain() function are parameters setting it to a C-SVM (-s 0) with an RBF kernel (-t 2). You can explore more options in the article linked above.
Thank you so much.I found it really informative to start with. This means that I need to inspect each pixel in detail for features , training and rest steps then apply svm on it?
(explanation to prior query) means that 10 20 is the value at a particular point or the number of pixels of start n end of a particular region like you explained?
Let's say you are trying to classify types of fruit. The training set has multiple fruit examples in it. So setting 1=Apple, 2=Orange, 3=Banana, you would have the following training set labels with two examples per fruit/class:
labels = [1;1;2;2;3;3];
Then you need to select features that can distinguish between your classes. For this fruit example, you might use mass and number of calories as features. So if apples are on average 112g and have 53kcals, oranges are 160g and have 99kcal, and bananas are on average 102g and have 97kcal, your training set features might be:
features = ...
[110,50; %apple example 1
120,60; %apple example 2
150,95; %orange example 1
165,105; %orange example 2
110,105; %banana example 1
98,95]; %banana example 2
So even though apple 1 and banana 1 have the same mass (110), they have very different caloric values and they can be distinguished. Likewise, even though orange 1 and banana 2 have the same number of calories (95), they have very different masses and can be distinguished. These are the relationships that the SVM will learn and allow it to make classifications. Then when you get a new example of a fruit with X mass and Y calories, you can figure out what it is.
How would I specify the 1=Apple, 2=Orange, 3=Banana thing in matlab and then use it in label matrix.Secondly, is the label matrix also training matrix?Thank you
as you gave example earlier labels = [1;0;1;2;1;2;0;2;0];
features = [11,12;1,2;15,14;27,29;10,9;23,24;2,4;22,24;6,5];
does it mean that there are three classes 0,1,2 and each class has feature starting frm 11 ending at 12.and together this 11 12 shows one image or sample.am I right?thank you
The training set is composed of two variables: the features matrix and the labels vector. Matlab doesn't know or care what the classes or features correspond to in reality. So you don't need to tell matlab that 1=Apple, etc. You only need to put the appropriate number in each row of the labels vector and then remember what each number corresponds to. The appropriate number is the correct label of that example. So if the third row is an example of an apple, then the third label should be 1.
You are correct about there being three classes in my previous example (let's say arbitrarily that 0=not a fruit, 1=Apple, 2=Pear). The first row of the labels vector is 1 and the first row of the features vector is [11,12]. This means that the first example is of class 1 (Apple), its value on the first feature (let's say mass) is 11, and its value on the second feature (let's say calories) is 12. The second row of the labels vector is 0 and the second row of the features matrix is [1,2]. This means that the second example is of class 0 (not a fruit), its value on the first feature (mass) is 1, and its value on the second feature (calories) is 2.
I am not sure what you mean by "each class has feature starting frm 11 ending at 12." However, I hope these examples will help you understand that each row in both variables corresponds to one example (i.e., the same example) and each column in the features matrix corresponds to a different feature.
Generally, training and testing sets should be different in order to evaluate the results obtained from a classifier. In addition, some researchers use a verification set to tune the parameters of the SVM. They initially train the system using the training set, verification set is then used to tune the parameters to obtain the best results. Considering the trained SVM with tuned parameters, the testing data is finally used to obtain the results and evaluate the performance of the SVM.