Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Fitted Error when using Sklearn's graphviz

Tags:

scikit-learn

when I am trying to export a random forest graph using the following command:

tree.export_graphviz(rnd_clf, out_file = None, feature_names = X_test[::1])

I receive the following error:

NotFittedError: This RandomForestClassifier instance is not fitted yet. 
Call 'fit' with appropriate arguments before using this method.

What I don't understand is why it keeps telling me this, even though I have fitted the random forest classifier using:

rnd_clf = RandomForestClassifier(  
             n_estimators=120,
             criterion='gini',
             max_features= None, 
             max_depth = 14 )

rnd_clf.fit(X_train, y_train)

and it works perfectly fine.

like image 936
F. K. Avatar asked Sep 13 '17 08:09

F. K.


3 Answers

(Only going by the docs; no personal experience)

You are trying to plot some DecisionTree, using a function which signature reads:

sklearn.tree.export_graphviz(decision_tree, ...)

but you are passing a RandomForest, which is an ensemble of trees.

That's not going to work!

Going deeper, the code internally for this is here:

check_is_fitted(decision_tree, 'tree_')

So this is asking for the attribute tree_ of your DecisionTree, which exists for a DecisionTreeClassifier.

This attribute does not exist for a RandomForestClassifier! Therefore the error.

The only thing you can do: print every DecisionTree within your RandomForest ensemble. For this, you need to traverse random_forest.estimators_ to get the underlying decision-trees!

like image 65
sascha Avatar answered Sep 21 '22 22:09

sascha


Like the other answer said, you cannot do this for a forest, only a single tree. You can, however, graph a single tree from that forest. Here's how to do that:

forest_clf = RandomForestClassifier()
forest_clf.fit(X_train, y_train)
tree.export_graphviz(forest_clf.estimators_[0], out_file='tree_from_forest.dot')
(graph,) = pydot.graph_from_dot_file('tree_from_forest.dot')
graph.write_png('tree_from_forest.png')

Unfortunately, there's no easy way to graph the "best" tree or an overall ensemble tree from your forest, just a random example tree.

like image 39
jss367 Avatar answered Sep 20 '22 22:09

jss367


enter code herefrom IPython.display import Image
from sklearn.`enter code here`externals.six import StringIO
from sklear`enter code here`n.tree import export_graphviz
import pydotplus
import pydot`enter code here`

    dt = DecisionTreeClassifier(criterion = 'entropy', max_depth = 3, min_samples_split = 20, class_weight = "balanced")
    dtree = dt.fit(ctg_x_train,ctg_y_train)
    
        k
    
    dot_data = StringIO()
    ctg_x_train_names = ctg_x_train.columns
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize = (12,12))
    
    export_graphviz(dtree, out_file=dot_data,filled = True, rounded = True,special_characters = True, feature_names = ctg_x_train_names)
    
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    
    (graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
    Image(graph.create_png())
like image 1
Mr.Vaibhav Patil Avatar answered Sep 22 '22 22:09

Mr.Vaibhav Patil