SVM Stats from MATLAB gives test error

Asked

Viewed 254 times

1

The following MATLAB code trains a binary classifier of MATLAB given a set of vectors and tests another set of vectors in this classifier:

%Número mínimo de iterações
optSVM = statset('MaxIter', 1000000);       

%treino do classificador
SVMtrainModel = svmtrain(training_vectors_matrix(:,2:end), training_vectors_matrix(:,1), 'kernel_function' ,  'linear', 'options', optSVM, 'tolkkt', 0.01);

%lê os vetores de teste 
TestV = csvread(test_file);

%Testa os vetores no classificador
TestAttribBin = svmclassify(SVMtrainModel, TestV(:,2:end))  

That is, it is a simple code that would run smoothly. Only here the training works normally, but when I test, MATLAB gives me the following error:

Subscript indices must either be real  positive integers or logicals.

Error in svmclassify (line 140) 
outclass= glevels(outclass(~unClassified),:); 

What would be the cause of this problem? I have looked for Nan values in training and testing and nothing. This code would normally run under normal conditions. What I have that might be causing this?

Crosspost: Matlab Stats Svm error in testing

  • What TestV(:,2:end) returns? By the error description it seems that the SVM code is not able to filter the elements in TestV. Can I provide a functional example (including the data)? Hence I can test here.

  • Hello Luiz. First thank you for the answer. Testv is a matrix with the test vectors. In the first column of Testv I have the real class of vectors (later used to calculate accuracy) and, from the second column on, we have the characteristic vectors. That is why Testattribbin = svmclassify(Svmtrainmodel, Testv(:,2:end)) tests the Testv matrix characteristic vectors in the Svmtrainmodel binary classifier. I will make the data and source available in a few moments. Thanks again.

  • Not at all @Mad. Well, I’m sorry if the question is too obvious, but you checked if the dimension of the characteristic vectors is the same in the training and test data, right?

  • 1

    Hi Luiz. These same vectors of characteristics have been converted to the format of Libsvm and in Libsvm is running normally the classification. However your question is very good so I need to check. Thanks again.

1 answer

2

The AP managed to response in the OS:

This should be solvable keeping my generic solution to this problem in mind.

1) Run dbstop code if error

It will now stop at the line you provided:

outclass= glevels(outclass(~unClassified),:);

2) Check for possible solutions.

In this case, I assume glevels and outclass are the two variables . The next thing to do would be to carefully examine everything that could be an index.

From the inside out:

The first index is ~unClassified, as the operation ~ did not fail, it is safe to say that this is now a logical vector.

The second and lastIndex is outclass(~unClassified), is more likely not to consist only numbers like 1,2,3, ... or true/false values of this.

The test if the values are all valid is quite simple, one of the two should contain:

To confirm that x values are logical: class(x) must return "logic" To confirm that x values are positive integers: isequal(x, max(1,round(abs(x)))) must return 'true'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.