MATLAB Examples

function call_generic_random_forests()

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name - call_generic_random_forests
% Creation Date - 7th July 2015
% Author - Soumya Banerjee
% Website - https://sites.google.com/site/neelsoumya/
%
% Description - Function to load data and call generic random forests function
%
% Parameters -
%	Input
%
%	Output
%               BaggedEnsemble - ensemble of random forests
%               Plots of out of bag error
%		Example prediction
%
% Example -
%		call_generic_random_forests()
%
% Acknowledgements -
%           Dedicated to my mother Kalyani Banerjee, my father Tarakeswar Banerjee
%				and my wife Joyeeta Ghose.
%
% License - BSD
%
% Change History -
%                   7th July 2015 - Creation by Soumya Banerjee
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


load fisheriris
X = meas;
Y = species;
BaggedEnsemble = generic_random_forests(X,Y,60,'classification')
predict(BaggedEnsemble,[5 3 5 1.8])
BaggedEnsemble = 

  TreeBagger
Ensemble with 60 bagged decision trees:
                    Training X:              [150x4]
                    Training Y:              [150x1]
                        Method:       classification
                 NumPredictors:                    4
         NumPredictorsToSample:                    2
                   MinLeafSize:                    1
                 InBagFraction:                    1
         SampleWithReplacement:                    1
          ComputeOOBPrediction:                    1
 ComputeOOBPredictorImportance:                    0
                     Proximity:                   []
                    ClassNames:        'setosa'    'versicolor'     'virginica'


ans =

  150×1 cell array

    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'setosa'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'virginica'
    'versicolor'
    'virginica'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'virginica'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'virginica'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'versicolor'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'versicolor'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'versicolor'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'versicolor'
    'versicolor'
    'virginica'
    'virginica'
    'virginica'
    'versicolor'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'
    'virginica'


Decision tree for classification
 1  if x3<2.6 then node 2 elseif x3>=2.6 then node 3 else setosa
 2  class = setosa
 3  if x3<4.75 then node 4 elseif x3>=4.75 then node 5 else versicolor
 4  class = versicolor
 5  if x1<5.95 then node 6 elseif x1>=5.95 then node 7 else virginica
 6  if x1<5.85 then node 8 elseif x1>=5.85 then node 9 else virginica
 7  if x4<1.75 then node 10 elseif x4>=1.75 then node 11 else virginica
 8  class = virginica
 9  class = versicolor
10  if x2<2.7 then node 12 elseif x2>=2.7 then node 13 else versicolor
11  class = virginica
12  class = virginica
13  if x1<7.05 then node 14 elseif x1>=7.05 then node 15 else versicolor
14  class = versicolor
15  class = virginica


BaggedEnsemble = 

  TreeBagger
Ensemble with 60 bagged decision trees:
                    Training X:              [150x4]
                    Training Y:              [150x1]
                        Method:       classification
                 NumPredictors:                    4
         NumPredictorsToSample:                    2
                   MinLeafSize:                    1
                 InBagFraction:                    1
         SampleWithReplacement:                    1
          ComputeOOBPrediction:                    1
 ComputeOOBPredictorImportance:                    0
                     Proximity:                   []
                    ClassNames:        'setosa'    'versicolor'     'virginica'


ans =

  cell

    'virginica'