Search code examples
scikit-learnpydot

Visualizing RandomForestRegression trees


I have run a RandomForestRegression model in sklearn and saved the output of my decisions trees (n_estimators=50) into 50 .dot files.

Now I want to save them so I can view them as actual trees.

I am trying this:

import pydotplus

dot_data=r'F:\Sheyenne\Random_Forest\my_tree0.dot'

graph = pydotplus.graph_from_dot_data(dot_data) 

graph.write_pdf(r'F:\Sheyenne\Random_Forest\my_tree0.pdf')

but this returns:

AttributeError: 'NoneType' object has no attribute 'write_pdf'

Solution

  • Looks like you are trying to load a file. Try this:

    import pydotplus
    
    dot_file=r'F:\Sheyenne\Random_Forest\my_tree0.dot'
    
    graph = pydotplus.graph_from_dot_file(dot_file) 
    
    graph.write_pdf(r'F:\Sheyenne\Random_Forest\my_tree0.pdf')