I have trained a Random Forest (bagged trees) model in matlab using the Classification toolbox. Does anyone know how I can know the number of trees the model used?

In the code I saved from the training, this is the part where the parameters are defined, but the number of trees isn't specified:

template = templateTree(...
'MaxNumSplits', 65);
classificationEnsemble = fitcensemble(...
predictors, ...
response, ...
'Method', 'Bag', ...
'NumLearningCycles', 30, ...
'Learners', template, ...
'ClassNames', categorical({'A'; 'B'; 'C'; 'D'; 'E'}));
predictorExtractionFcn = @(t) t(:, predictorNames);
ensemblePredictFcn = @(x) predict(classificationEnsemble, x);
trainedClassifier.predictFcn = @(x) ensemblePredictFcn(predictorExtractionFcn(x));

Thank you in advance!

  • $\begingroup$ Hi Johanna, welcome to the site. This site does not solely focuses on programming questions. Since this is a specific programming question, it might be helpful to post your question In stackoverflow or directly on Matlab support website. $\endgroup$
    – forecaster
    Aug 20, 2020 at 18:53
  • $\begingroup$ Thank you for the advice! $\endgroup$
    – Johanna
    Aug 21, 2020 at 8:50

1 Answer 1


Looks like they use NumLearningCycles as the name of the parameter for the number of base models to use. So in your case it is 30.

There is some documentation here for further review if needed! https://www.mathworks.com/help/stats/fitrensemble.html

  • $\begingroup$ Thank you so much! I had already read that whole document, and had failed to understand that NumLearningCycles was the number of trees in the model... thank you :) $\endgroup$
    – Johanna
    Aug 21, 2020 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.