Visualization of decision tree

8 views (last 30 days)
Mojtaba Zarei
Mojtaba Zarei on 19 Oct 2021
Answered: Aneela on 24 Apr 2024
I have a table in which all of its columns are categorical variables, and the label associated with them is also a categorical variable. I use this piece of code to fit a tree to the data:
Mdld = fitctree(X,Ycat,'MaxNumSplits',5,'PredictorSelection','curvature',...
'CrossVal','on');
when I plot the tree, the variables will be converted to a ordinal variable which is hard to track back what is the original variables that I had in table X. You can see the results here:
view(Mdld.Trained{1},'Mode','graph')
As you see, the labels are correct, but for example, in the X table, I have "Small" and "Large" categories for vessel radius, but I get 1 and 2 labels instead, for which I event do not know readily which one is which!
I could not find a way to keep the labels on the node. Can anyone help me to do so?

Answers (1)

Aneela
Aneela on 24 Apr 2024
Hi Moztaba Zarei,
I tried the below example in MATLAB.
numObservations = 100;
% Define the categorical variables
VehicleType = categorical(randi([1, 3], numObservations, 1), 1:3, {'Car', 'Truck', 'Bike'});
Color = categorical(randi([1, 3], numObservations, 1), 1:3, {'Red', 'Blue', 'Green'});
Size = categorical(randi([1, 3], numObservations, 1), 1:3, {'Small', 'Medium', 'Large'});
% Combine into a table
data = table(Size, Color);
% Display the first few rows of the dataset
head(data)
tree=fitctree(data,VehicleType);
view(tree,'Mode','graph');
I got the below tree:
I would suggest you check the preprocessing steps of the VesselRadius and make sure the categorical values are not "1" and "2".
For more information on Categorical Predictors refer to the following MathWorks Documentation: https://in.mathworks.com/help/stats/fitctree.html#namevaluepairs:~:text=%7C%20%27all%27-,Categorical%20predictors,-list%2C%20specified%20as

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!