Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

support vector machines in matlab

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
like image 917
edgarmtze Avatar asked Feb 12 '11 05:02

edgarmtze


People also ask

What is support vector machine in Matlab?

A support vector machine (SVM) is a popular machine learning technique that delivers highly accurate, compact models. The learning algorithm optimizes decision boundaries to minimize classification errors and transformations of the feature space using kernel functions that help separate classes.

What is support vector machine explain in detail?

A support vector machine (SVM) is a type of deep learning algorithm that performs supervised learning for classification or regression of data groups. In AI and machine learning, supervised learning systems provide both input and desired output data, which are labeled for classification.

What is support vector machine types?

There are two different types of SVMs, each used for different things: Simple SVM: Typically used for linear regression and classification problems. Kernel SVM: Has more flexibility for non-linear data because you can add more features to fit a hyperplane instead of a two-dimensional space.

What is Fitcsvm in Matlab?

Description. fitcsvm trains or cross-validates a support vector machine (SVM) model for one-class and two-class (binary) classification on a low-dimensional or moderate-dimensional predictor data set.


2 Answers

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
like image 85
Amro Avatar answered Sep 19 '22 15:09

Amro


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);
like image 34
Jacob Avatar answered Sep 19 '22 15:09

Jacob