Search code examples
pythongraphviz

TypeError: <sklearn.tree._tree.Tree object at 0x1a17272030> is not an estimator instance


Can someone help me to understand why I am getting this error please : TypeError: is not an estimator instance.

Here is my code :

from sklearn.tree import DecisionTreeClassifier
from sklearn import tree

model = DecisionTreeClassifier(max_depth = 3,criterion='entropy')
model.fit(x_train,y_train)

import pydotplus
feature_names = [key for key in df]
dot_data= tree.export_graphviz(model.tree_, out_file=None, feature_names=feature_names) 
graph = pydotplus.graph_from_dot_data(dot_data) 
graph.write_pdf("mines.pdf") 

Solution

  • you should put your code in correct format. For example:

    from sklearn.tree import DecisionTreeClassifier
    from sklearn import tree
    
    model=tree.DecisionTreeClassifier(max_depth=3,criterion='entropy')
    model.fit(x_train,y_train)
    
    #etc.
    

    And when you export to graphviz use model (not model.tree_)

    dot_LasDataOrig = tree.export_graphviz(model,  out_file=None, feature_names=feature_names)
    

    Probably this line gives you the TypeError: is not an estimator instance.