support vector machines in matlab

后端 未结 2 1722
心在旅途
心在旅途 2020-11-29 04:06

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 atri         


        
相关标签:
2条回答
  • 2020-11-29 04:50

    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);
    
    0 讨论(0)
  • 2020-11-29 05:03

    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
    
    0 讨论(0)
提交回复
热议问题