我创建了一个决策树,并尝试按照答案(在scikit learn中可视化决策树)在python中可视化它,但仍然不起作用:
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()
我的错误是:
AttributeError: 'DecisionTreeClassifier' object has no attribute
'export_graphviz'
有大师能帮我解决这个问题吗?
export_graphviz
是sklearn.tree
的函数,不是来自分类器:
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file=dotfile, feature_names=X.columns)