Classification by one class using LIBSVM. MATLAB

I am trying to implement multiclass classification vs rest using LIBSVM.

This link was useful http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/ovr_multiclass/ but I get an error in the function "ovrpredict ()".

The function is as follows:

function [pred, ac, decv] = ovrpredict(y, x, model)

labelSet = model.labelSet;
labelSetSize = length(labelSet);
models = model.models;
decv= zeros(size(y, 1), labelSetSize);

for i=1:labelSetSize
  [l,a,d] = svmpredict(double(y == labelSet(i)), x, models{i});
  decv(:, i) = d * (2 * models{i}.Label(1) - 1);             % ERROR IN THIS LINE
end
[tmp,pred] = max(decv, [], 2);
pred = labelSet(pred);
ac = sum(y==pred) / size(x, 1);

The error message I get is this Reference to non-existent field 'Label'.

Any suggestions would be really helpful.


EDIT 1

Code used to call functions: \

[trainY trainX]=libsvmread('libfacecombine.train');
[testY testX]=libsvmread('libfacetest.train');
model=ovrtrain(trainY,trainX,'-c 8 -g 4');
[~,accuracy,~]=ovrpredict(testY,testX,model);

The training and testing data with "libfacecombine.train" and "libfacetest.train" are written from CSV files:

f1=createdabase(f);     % where createdatabase is a function to read various images from a folder and arrange into 1D array
[sig1 mn1]=pcam(f1);    % where pcam is a function to find 'pca'(sig1) and 'mean'(mn1) of the data

%labelling is done this way:
%Positive class
        label=[];
        for i=1:length(sig1)
            for j=1:1
                label(i,j)=+1;
            end
        end
    csvwrite('face1.csv',[label sig1]);

%Negative class
        label1=[];
            for i=1:length(sig2)             % sig2 obtained in same way as sig1
                for j=1:1
                    label1(i,j)=-1;
                end
            end
        csvwrite('face2.csv',[label sig2]);

Using the append mode, both of these files are combined and converted to .train files. The same is done for test data.

EDIT 2

5 . : 1: +1 4 Face 1 -1 4 Face 1 ( 2,3,4 5). 2: +2 4 Face 2 -2 4 Face 2 ( 1,3,4 5).... 5: +5 4 5 -5 4 Face 5 ( 1,2,3 4). .csv , .train. .

1 i.e +1 CSV , .train. , . , , :

Accuracy=92%(12/13)classification; 
Accuracy=61%(8/13)classification;
Accuracy=100%(13/13)classification;
Accuracy=100‌​%(13/13)classification;
Accuracy=100%(13/13)classification;
Accuracy=100%(13/13)cla‌​ssification; 

6 , 5 ?

+4
2

- , . , . , , , - , , 90% , .

MATLAB.


, , . . clear .


libsvm, ovr_multiclass, script, :

clear

% random train and test data
trainX = rand(10, 4);
trainY = randi(4, 10, 1);
testX = rand(10, 4);
testY = randi(4, 10, 1);

model=ovrtrain(trainY,trainX,'-c 8 -g 4'); 
[~,accuracy,~]=ovrpredict(testY,testX,model);

, ? . , .


models

, :

decv(:, i) = d * (2 * models{i}.Label(1) - 1);             % ERROR IN THIS LINE

models{i}.Label(1). models i th. , i - Label. , Label , . models model, ovrpredict.

script , MATLAB:

>> models = model.models

models = 

    [1x1 struct]
    [1x1 struct]
    [1x1 struct]
    [1x1 struct]

>> models{1}

ans = 

    Parameters: [5x1 double]
      nr_class: 2
       totalSV: 6
           rho: -1.2122
         Label: [2x1 double]
    sv_indices: [6x1 double]
         ProbA: []
         ProbB: []
           nSV: [2x1 double]
       sv_coef: [6x1 double]
           SVs: [6x4 double]

>> models{1}.Label

ans =

     0
     1

, ? , .


, , , :

dbstop if error

MATLAB , .

( , , ). . >> K>>.

, model. , , . , . models{i}.Label(1), 2 * models{i}.Label(1) - 1.

dbquit dbclear if error, .

(. - , , !)


:

  • MATLAB ? R2013a

  • which ovrpredict MATLAB, ? (.. ovrpredict.m, )

  • ovrpredict.m ( which ovrpredict) , ? , , , , . .

+2

, , :

ovrtrain labelSet.

overpredict label.

, :

: ,

, , , , label labelSet .

, 1 3, .

+1

Source: https://habr.com/ru/post/1538527/


All Articles