Domanda

I am using the Matlab 2012 svm included in the stats package. I have a binary classification problem where I train a set of vectors and test another set of vectors as the following matlab code shows:

%set the maximum number of iterations
optSVM = statset('MaxIter', 1000000);       

%train the classifier with a set of feature vectors
SVMtrainModel = svmtrain(training_vectors_matrix(:,2:end), training_vectors_matrix(:,1), 'kernel_function' ,  'linear', 'options', optSVM, 'tolkkt', 0.01);

%read the test vectors 
TestV = csvread(test_file);

%Test the feature vectors in the built classifier
TestAttribBin = svmclassify(SVMtrainModel, TestV(:,2:end))  

It' s a quite simple code and would run normally. The training runs ok, but when I test the following error happens:

Subscript indices must either be real  positive integers or logicals.

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

So, are my feature vectors with any problem? If I run this same code in different feature vectors (training and testing vectors) the code runs ok. I already checked the feature vectors and there are no NaNs. What should be the cause of this problem?

È stato utile?

Soluzione

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

1) Run the code with dbstop if error

It will now stop at the line that you provided:

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

2) Check the possible solutions.

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

Starting 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), this one is most likely not consisting of only numbers like 1,2,3,... or true,false values.

The test whether the values are all valid is quite simple, one of these two should hold:

  • To confirm that the values in x are logical: class(x) should return 'logical'
  • To confirm that the values in x are real positive integers: isequal(x, max(1,round(abs(x)))) should return 'true'.

Altri suggerimenti

This problem can be solve if you remove your NaN rows or data:

    Features(~any(~isnan(Features), 2),:)=[];

maybe you have complex numbers too, then use this code:

    Features3(any(isnan(Features3),2),:)=0;
    Features3 =real(Features3);

first line, make all NaN values turn to zero and the second line turns all complex numbers to be real.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top