-1

I created a decision tree and tried to follow an answer(Visualizing decision tree in scikit-learn) to visualize it in python,but still don't work:

import pandas as pd
score_v2 = pd.read_csv("C:/TEST_RF_CSV_simple.csv",encoding = "cp950")

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

score_X = score_v2
score_y = score_v2.buy_lf
X_train, X_test, y_train, y_test = train_test_split(
score_X, score_y, test_size=0.3)

from sklearn.tree import DecisionTreeClassifier
tree=DecisionTreeClassifier(criterion = 'entropy', max_depth=3, 
random_state=0)
tree.fit(X_train, y_train)
tree_1 = tree.fit(X_train, y_train)

from sklearn.tree import export_graphviz
dotfile = open("D:/dtree2.dot", 'w')
tree.export_graphviz(dtree, out_file = dotfile, feature_names = 
X.columns)
dotfile.close()

my error was:

AttributeError: 'DecisionTreeClassifier' object has no attribute 
'export_graphviz'

Could any masters help me with solving the problem?

kate
  • 1
  • 1
  • 1
  • 2
    Possible duplicate of [Python Decision Tree GraphViz](https://stackoverflow.com/questions/34144134/python-decision-tree-graphviz) – sync11 Nov 26 '18 at 09:48

1 Answers1

1

export_graphviz is a function of sklearn.tree, not from the classifier:

from sklearn.tree import export_graphviz
export_graphviz(tree, out_file=dotfile, feature_names=X.columns)
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62