我正在尝试为它创建决策树和图表。
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 {
您应该在export_graphviz
函数中提供树对象,而不是管道对象。为此,您必须从管道中获取树分类器,并将其传递到export\u graphviz
请尝试使用以下最后几行运行代码:
with open("./plots/ritesh.dot", "w") as f:
export_graphviz(dt_old.named_steps['classifier'], out_file=f)