我创建了一个决策树,并尝试遵循答案(Visualizing decision tree in 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'
任何大师都可以帮助我解决问题吗?
答案 0 :(得分:0)
export_graphviz
是sklearn.tree
的功能,而不是来自分类器的功能:
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file=dotfile, feature_names=X.columns)