Build trees for a random forest in Python using Scikit-Learn

I want to build a random forest decision tree. So, I am creating the following code:

clf = RandomForestClassifier(n_estimators=100) import pydotplus import six from sklearn import tree dotfile = six.StringIO() i_tree = 0 for tree_in_forest in clf.estimators_: if (i_tree <1): tree.export_graphviz(tree_in_forest, out_file=dotfile) pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png('dtree'+ str(i_tree) +'.png') i_tree = i_tree + 1 

But it does not generate anything .. Do you have an idea how to build a decision tree from a random forest?

Thanks,

+15
source share
2 answers

Assuming your Random Forest model is already installed, you must first import the export_graphviz function export_graphviz :

 from sklearn.tree import export_graphviz 

In your loop, you can do the following to create a dot file

 export_graphviz(tree_in_forest, feature_names=X.columns, filled=True, rounded=True) 

The next line generates a png file

 os.system('dot -Tpng tree.dot -o tree.png') 
+22
source

You can draw one tree:

 from sklearn.tree import export_graphviz from IPython import display from sklearn.ensemble import RandomForestRegressor m = RandomForestRegressor(n_estimators=1, max_depth=3, bootstrap=False, n_jobs=-1) m.fit(X_train, y_train) str_tree = export_graphviz(m, out_file=None, feature_names=X_train.columns, # column names filled=True, special_characters=True, rotate=True, precision=0.6) # complimentary line, you can exclude it if you don't need it str_tree = re.sub('Tree {', f'Tree {{ size={size}; ratio={ratio}', str_tree) display.display(str_tree) 
0
source

Source: https://habr.com/ru/post/1011515/


All Articles