Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

error using classperf

Asked by FIR on 11 Apr 2012
I have a code
data=[0.5 .3 0.6 0.5;0.2 0.3 0.5 0.9;0.1 0.7 0.2 0.8;0.5 .3 0.6 0.5;0.2 0.3 0.5 0.9;0.1 0.7 0.2 0.8]
TrainData=data(:,1:2)
 TestVector=data(:,3:4)
 groups=[0;1;1;0;1;1]
   cvFolds = crossvalind('kfold', groups, 10);  %# get indices of 10-fold CV %# get indices of 10-fold CV
  cp = classperf(groups);  
  for i = 1:10   
%   testIdx = (cvFolds == i);                %# get indices of test instances
%    trainIdx = ~testIdx; 
   Training=(data(:,1:2));
   TestVector=data(:,3:4)
 Group=groups;
 TreeObject=TreeBagger(50,Training,Group,'NVarToSample','all');
   [YFIT,scores] = predict(TreeObject,TestVector);
  %cp = classperf(cp, YFIT, testIdx)
  cp = classperf(cp, YFIT)
end

I get error

Error using classperf (line 230)
When the class labels of the CP object are numeric, the output
of the classifier must be all non-negative integers or NaN's.
Error in yass (line 30)
cp = classperf(cp, YFIT)

0 Comments

FIR

Tags

Products

No products are associated with this question.

1 Answer

Answer by Ilya on 11 Apr 2012
Accepted answer

See my response in http://www.mathworks.com/matlabcentral/answers/34771-classifier

In your code snippet, the error is caused by TreeBagger returning predicted class labels as a cell array of strings, while the class labels (groups variable) passed to classperf are numeric. To avoid this, convert groups to a cell array of strings:

groups = cellstr(num2str(groups))

2 Comments

FIR on 12 Apr 2012

am getting the same error as before

Error using classperf (line 230)
When the class labels of the CP object are numeric, the output
of the classifier must be all non-negative integers or NaN's.

Error in ya (line 23)
cp = classperf(cp, YFIT)

Ilya on 12 Apr 2012

Well, the error says "When the class labels of the CP object are numeric..." This means you have not converted groups to a cell array before passing to classperf.

Ilya

Contact us