在给出错误的决策树期间导出 graphviz
Posted
技术标签:
【中文标题】在给出错误的决策树期间导出 graphviz【英文标题】:export graphviz during decision tree giving error 【发布时间】:2016-03-03 17:38:54 【问题描述】:我正在尝试为其创建决策树和图表。
print "\nCreating Pipeline for the analyzing and training ..."
dt_old = Pipeline([
('bow', CountVectorizer(analyzer=split_into_lemmas)), # strings to token integer counts
('tfidf', TfidfTransformer()), # integer counts to weighted TF-IDF scores
('classifier', DecisionTreeClassifier(min_samples_split=20, random_state=99)), # train on TF-IDF vectors w/ DecisionTree classifier
])
print("pipeline:", [name for name, _ in dt_old.steps])
print("-- 10-fold cross-validation , without any grid search")
dt_old.fit(msg_train, label_train)
scores = cross_val_score(dt_old, msg_train, label_train, cv=10)
print "mean: :.3f (std: :.3f)".format(scores.mean(), scores.std())
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
with open("./plots/ritesh.dot", "w") as f:
export_graphviz(dt_old, out_file=f)
每当我尝试为决策树创建点文件时,都会出现以下错误。
Creating Pipeline for the analyzing and training ...
('pipeline:', ['bow', 'tfidf', 'classifier'])
-- 10-fold cross-validation , without any grid search
mean: 0.960 (std: 0.007)
Traceback (most recent call last):
File "DecisionTree.py", line 192, in <module>
main()
File "DecisionTree.py", line 128, in main
export_graphviz(dt_old, out_file=f)
File "/Users/ritesh/anaconda/lib/python2.7/site-packages/sklearn/tree/export.py", line 128, in export_graphviz
recurse(decision_tree.tree_, 0, criterion=decision_tree.criterion)
AttributeError: 'Pipeline' object has no attribute 'tree_'
没有管道我可以生成点文件,但管道没有成功。我错过了什么吗?
生成的输出文件只是:
digraph Tree
【问题讨论】:
【参考方案1】:您应该在export_graphviz
函数中提供树对象,而不是管道对象。为此-您必须从管道中获取树分类器并将其传递给export_graphviz
尝试使用最后几行来运行您的代码:
with open("./plots/ritesh.dot", "w") as f:
export_graphviz(dt_old.named_steps['classifier'], out_file=f)
【讨论】:
以上是关于在给出错误的决策树期间导出 graphviz的主要内容,如果未能解决你的问题,请参考以下文章
Graphviz绘制决策树基于Graphviz绘制由字典形式表示的决策树图像