How to set tree arguments in TreeBagger
8 views (last 30 days)
Show older comments
Hi.
I'm using TreeBagger for classification and I want to control the depth of the trees in it.
I have read that you can control the depth of the trees using tree parameters, MinLeafSize, MinParentSize and MinParentSize in here https://la.mathworks.com/help/stats/fitctree.html#bt6cr7t_sep_shared-MaxNumSplits.
Also in the TreeBagger documentation says that it has an argument TreeArguments that receive a Cell array of arguments for fitctree or fitrtree. These arguments are used by TreeBagger when growing new trees for the ensemble.
I'm trying to set this property but I get the next error with the next code.
maxNumSplits = 3; %Maximal number of decision splits, positive integer
minLeafSize = 3; %Mini um number of leaf node observations, positive integer
minParentSize = 3; %Minimum number of brach node observation, positive integer
numberPredictorsToSample = 3; %number of random feature for each decision, default = squareroot of the total number of features
numTrees = 500; %Scalar value equal to the number of desicion trees
treeArguments = {'MaxNumSplits',maxNumSplits,'MinLeafSize',minLeafSize,'MinParentSize',minParentSize};
RF = TreeBagger(numTrees,trainingData,trainingClass,'TreeArguments',treeArguments);
The Error I get is
Error using internal.stats.parseArgs (line 42) Invalid parameter name: TreeArguments.
Error in classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 115) [W,predictornames,responsename,catpreds] = ...
Error in ClassificationTree.prepareData (line 471) [X,Y,W,dataSummary] = ...
Error in TreeBagger/init (line 1190) [bagger.X,y,bagger.W,bagger.DataSummary,classSummary] = ...
Error in TreeBagger (line 531) bagger = init(bagger,X,Y,makeArgs{:});
Error in variabilidad (line 42) RF = TreeBagger(numTrees,trainingData,trainingClass,'TreeArguments',templateTree);
0 Comments
Answers (1)
luis barbosa
on 17 Nov 2018
Edited: luis barbosa
on 17 Nov 2018
Dear Fabian,
I saw your code and there are some errors there.
First, the TreeBagger (function/class) does not accept as argument 'TreeArguments'. So it is not possible to creat a cell-array (the case of your code), or a structure and simple pass it to the Trebagger as an argument.
One way to run your code is the following:
maxNumSplits = 3; %Maximal number of decision splits, positive integer
minLeafSize = 3; %Mini um number of leaf node observations, positive integer
minParentSize = 3; %Minimum number of brach node observation, positive integer
numberPredictorsToSample = 3; %number of random feature for each decision, default = squareroot of the total number of features
numTrees = 500; %Scalar value equal to the number of desicion trees
method = 'regression'; %or 'classification'
%% training a TreeBagger.
% It is a random forests since the numberPredictorsToSample is less
% than the amount of inputs in the trainingData, used to train the RF.
% Otherwise, we have a simple bagged ensemble of trees.
% assuming:
% - trainingData is a table
% - y_label is a string with the output name
RF = TreeBagger(numTrees,trainingData,y_label,...
'Method',method,... %you have to say whether it is a regression or classification problem
'MaxNumSplits',maxNumSplits,...
'MinLeafSize',minLeafSize,...
'NumPredictorsToSample',numberPredictorsToSample);
I left out the 'MinParentSize', because an error occurred, namely:
"Error using TreeBagger/init (line 1414)
TreeBagger does not accept 'MinParentSize' and 'SplitMin' input parameters. Use 'MinLeafSize'."
I didn't try to solve this problem. Perhaps, a possible solution is to set this parameter by using templateTree, and pass it to TreeBagger, something like this:
t = templateTree('MinParentSize',number);
RF = TreeBagger(numTrees,trainingData,y_label,...
'Method',method,... %you have to say whether it is a regression or classification problem
'MaxNumSplits',maxNumSplits,...
'MinLeafSize',minLeafSize,...
'NumPredictorsToSample',numberPredictorsToSample,...
'Learners',t); %passing the created templateTree object
0 Comments
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!