Did you know that Decision Forests (or Random Forests, I think they are pretty much the same thing) are implemented in MATLAB? In MATLAB, Decision Forests go under the rather deceiving name of TreeBagger.
Here’s a quick tutorial on how to do classification with the TreeBagger class in MATLAB.
% Since TreeBagger uses randomness we will get different results each % time we run this. % This makes sure we get the same results every time we run the code. rng default % Here we create some training data. % The rows< represent the samples or individuals. % The first two columns represent the individual's features. % The last column represents the class label (what we want to predict) trainData = [ ... [6, 300, 1]; [3, 300, 0]; [8, 300, 1]; [11, 2000, 0]; [3, 100, 0]; [6, 1000, 0]; ]; features = trainData(:,(1:2)) classLabels = trainData(:,3) % How many trees do you want in the forest? nTrees = 20; % Train the TreeBagger (Decision Forest). B = TreeBagger(nTrees,features,classLabels, 'Method', 'classification'); % Given a new individual WITH the features and WITHOUT the class label, % what should the class label be? newData1 = [7, 300]; % Use the trained Decision Forest. predChar1 = B.predict(newData1); % Predictions is a char though. We want it to be a number. predictedClass = str2double(predChar1) % predictedClass = % 1 % So we predict that for our new piece of data, we will have a class label of 1 % Okay let's try another piece of data. newData2 = [7, 1500]; predChar2 = B.predict(newData2); predictedClass2 = str2double(predChar2) % predictedClass2 = % 0 % It predicts that the new class label is a 0. |
There’s an excellent tutorial in the MATLAB documentation here that covers a lot more.
Thank You a Lot!!!!
Hello, thank you for the example.
I am trying the treebagger function on a simple regression example, but it is always predicting the same response whatever the input was. Why do you think this is happening? Can someone post a very simple matlab code for a regression example using treebagger?
Thank you in advance
I’m not sure offhand sorry.
Sir, How can I see the complete tree when use TreeBagger method??
Hi MekalaB, you can view the first tree like this:
view(B.Trees{1})
Hi Jer,
Thanks for the help. Actually I am working on the segmentation of organ from ct scan. But I am not able to understand what will be the features like shape or size.. Can you guide me for this?
Thanks and Regards
Hi Jer,
Thanks for sharing such a nice tutorial. It helps me a lot to understand the basic concepts in MATLAB. But I still dont understand how we can use it with image processing.??
Can you please help me.
Thanks and Regards
Hello Richa,
Did you have a particular image processing problem in mind?
One simple example would be if you had an image with some object in it you wanted to detect. Say you then labeled each pixel in the image as
0
or1
where a0
represents the background, and a1
represents the object. You could then train a Random Forest by giving the pixel values (i.e. the intensities) asfeatures
(the variable in the above example) and the0
or1
label as theclassLabels
(see variable in the above example).Then you could take a new image, and pass in ONLY the pixel values (the
newData1
variable in the example above) to the Random Forest. The Random Forest would then try and predict what the correct label (0
or1
) of the pixels should be based only on the image features.Thanks for this great demo on Random Forest using MATLAB, it was really helpful. Thanks again.
Glad it helped 🙂