8
$\begingroup$

Recently, I have noticed that there is a method sklearn.tree.export_graphviz documented here.

However, I do not know how I can apply it to a RandomForestClassifier.

I have tried the following naive code but it does not work, and I do not know how I can get one of the trees from a RandomForestClassifier:

print('Training...')
forest = RandomForestClassifier(n_estimators=100)
forest = forest.fit( train_data[0::,1::], train_data[0::,0] )


print('Predicting...')
output = forest.predict(test_data).astype(int)

if sys.version_info >= (3,0,0):
    predictions_file = open("myfirstforest.csv", 'w', newline='')
else:
    predictions_file = open("myfirstforest.csv", 'wb')


tree.export_graphviz(forest, out_file='tree.dot')
$\endgroup$
13
$\begingroup$

Your RandomForest creates 100 tree, so you can not print these in one step. Try iterate over the trees in the forest and print them out one by one:

from sklearn import tree
i_tree = 0
for tree_in_forest in forest.estimators_:
    with open('tree_' + str(i_tree) + '.dot', 'w') as my_file:
        my_file = tree.export_graphviz(tree_in_forest, out_file = my_file)
    i_tree = i_tree + 1

If you want to know the actual parameters of the trees like splitting attribute (feature), splitting value (threshold), node samples (n_node_samples) etc., you can use print getmembers(tree_in_forest.tree_) in the for cycle. To use one of these parameters, eg. threshold, use this: tree_in_forest.tree_.threshold It returns with a list.

$\endgroup$
2
  • $\begingroup$ What is getmembers? $\endgroup$
    – Jarad
    Jun 9 '17 at 6:50
  • $\begingroup$ @Jarad It's from inspect but you don't really need it here. $\endgroup$
    – Matthew
    Dec 12 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.