Search code examples
matlabmatlab-figuredecision-tree

Matlab: Change variable resolution and names for viewing regression trees


Using treeMine = fitctree(....) I can generate a decision tree but the tree is very big, and therefore very difficult to convey information, when using view(treeMine,'Mode','Graph') enter image description here

Therefore my question is if it is possible to change variable names x1-x9 to other names to make it human understandable and if I could force the numbers to be represented by engineering notation meaning 10e3.

Does anybody know how this can be done?

Minimal Example

Minimal example can be to use Matlabs own car example:

load carsmall
idxNaN = isnan(MPG + Weight);
X = Weight(~idxNaN);
Y = MPG(~idxNaN);
n = numel(X);

rng(1) % For reproducibility
idxTrn = false(n,1);
idxTrn(randsample(n,round(0.5*n))) = true; % Training set logical indices
idxVal = idxTrn == false;                  % Validation set logical indices

Mdl = fitrtree(X(idxTrn),Y(idxTrn));
view(Mdl,'Mode','graph')

How do you then specify the value resolution and variable name


Solution

  • About the names: It's a bit a poor example because you use only one predictor (weight), but you can change the name with the 'PredictorNames' name-value pair, e.g.

    Mdl = fitrtree(X(idxTrn),Y(idxTrn),'PredictorNames',{'weight'});
    

    If you were to use more predictors you just have to add more elements to the cell array, e.g.

    'PredictorNames',{'weight','age','women'}
    

    I don't know about the numbers tough.