2

Can someone help to solve and if possible explain my mistake. I have a two numeric matrices for using it in classification tree

x: data matrix <2422x39 double>

y: column vector, class label for each instance <2422x1 double>

I'm doing:

t = classregtree(x, y, 'method','classification');
yPredicted = eval(t, x);
cm = confusionmat(y,yPredicted); // error

Error using ==> confusionmat at 52

G and GHAT need to be the same type.

Tree succesfully built. But I cannot get a confusion matrix for that example

I have read that post to write above code Decision Tree in Matlab

If I use exactly same example from link, its working but when I use my own its not working. Same steps I took for building regression tree ( t = classregtree(x, y) ) and no error in confusionmat() function. Please explain what I'm doing incorrectly.

Thanks in advance

Community
  • 1
  • 1
ikhtiyor
  • 504
  • 5
  • 15

1 Answers1

2

It seems to me in your case, eval(t,x) returns cells of char type, while your x and y come with "double" type instead of "char".

The reason the code in Decision Tree in Matlab works is because:

y = strcat(Origin,{});

returns y that is a cell with "char". Thus the argument G and GHAT have the same type.

So, select one that suits your problem:


Approach A: convert yPredicted to numeric matrix

Edit this line :

yPredicted = eval(t, x);

to :

yPredicted = str2num( cell2mat( eval(t, x) ) );

Approach B: convert y to cell of char before calling confusionmat()

 y = num2cell( num2str(y) )
Community
  • 1
  • 1
EwyynTomato
  • 4,009
  • 1
  • 31
  • 39