I saw the help in Matlab, but they have provided an example without explaining how to use the parameters in the 'classregtree' function. Any help to explain the use of 'classregtree' with its parameters will be appreciated.
The documentation page of the function classregtree is self-explanatory...
Lets go over some of the most common parameters of the classification tree model:
A complete example to illustrate the process:
%# load data
load carsmall
%# construct predicting attributes and target class
vars = {'MPG' 'Cylinders' 'Horsepower' 'Model_Year'};
x = [MPG Cylinders Horsepower Model_Year]; %# mixed continous/discrete data
y = cellstr(Origin); %# class labels
%# train classification decision tree
t = classregtree(x, y, 'method','classification', 'names',vars, ...
'categorical',[2 4], 'prune','off');
view(t)
%# test
yPredicted = eval(t, x);
cm = confusionmat(y,yPredicted); %# confusion matrix
N = sum(cm(:));
err = ( N-sum(diag(cm)) ) / N; %# testing error
%# prune tree to avoid overfitting
tt = prune(t, 'level',3);
view(tt)
%# predict a new unseen instance
inst = [33 4 78 NaN];
prediction = eval(tt, inst) %# pred = 'Japan'
The above classregtree
class was made obsolete, and is superseded by ClassificationTree
and RegressionTree
classes in R2011a (see the fitctree
and fitrtree
functions, new in R2014a).
Here is the updated example, using the new functions/classes:
t = fitctree(x, y, 'PredictorNames',vars, ...
'CategoricalPredictors',{'Cylinders', 'Model_Year'}, 'Prune','off');
view(t, 'mode','graph')
y_hat = predict(t, x);
cm = confusionmat(y,y_hat);
tt = prune(t, 'Level',3);
view(tt)
predict(tt, [33 4 78 NaN])