Visualize or Print Random Forest Algorithm Model

As a machine learning engineer you may have created the Random Forest Algorithm Model, but have you ever tried to visualize it.

If not , this is the post for you.

In this post we will learn how to Visualize or Print Random Forest Algorithm Model in Jupyter notebook.

Let us import the required library:

from IPython.display import Image  
from sklearn import tree
import pydotplus

After that we will use the export_graphviz method of tree class and pass the names of features and class used to Train the Random Forest Regression Model named RandomForestRegModel.

If you are not aware how to create a Train the Random Forest Classifier Model. Please follow the link above to read about that. You may also like to read.

tree_count = 0
for tree_in_random_forest in RandomForestRegModel.estimators_:
    if (tree_count ==1):        
        rfr_file = tree.export_graphviz(tree_in_random_forest, out_file=None)
        rfr_graph = pydotplus.graph_from_dot_data(rfr_file)        
    tree_count = tree_count + 1
Image(rfr_graph.create_png())

Using the above code we can Visualize the first tree of the Random Forest Model used.

Random Forest Regression Model Visualization in Python
Random Forest Regression Model Visualization in Python

I hope you enjoyed this article and can start using some of the techniques described here in your own projects soon. Cheers !!

Advertisements

Leave a Reply