How to Visualize Decision Tree from a Random Forest Model? | by Satyam Kumar | Towards Data Science

Random Forest or Random Decision Forest is a supervised ensemble machine learning technique, for training classification and regression models. The training algorithm of random forest uses bagging or bootstrap aggregation technique, using decision tree as base learners. Random Forest uses randomization to reduce the bias in decision tree models.
A Decision Tree is a supervised machine learning technique, where the data is split according to a certain parameter and forms a tree-like structure. The tree can be explained using decision nodes and leaves.
After training a robust model and evaluating the performance of the model, the question arises, what is the cause of the prediction. When it comes to predictive modeling, you need to trade-off, it the business requirement to just get the prediction or the cause of prediction.
Model Interpretability includes looking under the hood of the model, figuring out which features are important to make the prediction. There are various model interpretation techniques to interpret the cause of predictions.
Random Forest uses the bagging technique to reduce the bias of its base learners that is decision trees. The Random Forest model can be considered as a black-box model, as the cause of predictions is very difficult to interpret. To interpret a random forest model, you need to understand the working of its base learners.
Dataset: We will be using Iris Dataset to demonstrate the visualization of random forest trees
n_estimators parameter in Random Forest Classifier determines the number of trees, the model will have as the base learners.
To visualize multiple decision trees for a random forest model, we will be using export_graphviz library. This library is used to export the decision tree in DOT format and generates a GraphViz representation of a decision tree.
To visualize the decision tree of a random forest, follow the steps:
Load the dataset
Train Random Forest Classifier model with n_estimator parameters as a number of base learners (decision trees).
model.estimators_: The collection of fitted sub-estimators or list of Decision Tree Classifiers with its defined parameters.
Save each Decision Tree model as a DOT file using export_graphviz library to create the visualization.
Print the visualization of each decision tree
For each node of the tree:1st line represents the condition (example: petal_length≤2.6)gini: gini score samples: total number of samples from all three classesvalue: distribution of samples amongst each target classclass: most probable prediction of target class label for that node
From the above visualization, we conclude the following points:
Petal Length is the most important feature, and petal_length≤2.6, result in setosa class label.
The tree node is split into the left (true condition) and right (false condition) nodes until we get the leaf node.
A random forest model can be considered as a black-box model, as it is difficult the interpret the cause of prediction. In this article, we have discussed how to visualize multiple decision trees that constitute a random forest model.
However, there is one limitation, if the number of decision trees is large, then visualizing each of them would be difficult. This technique works best to interpret the model to business folks for random forest model having less number of decision trees.
[1] Export Graph Viz Documentation (scikit-learn): https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html
Thank You for Reading