我正在尝试为它创建决策树和图形。
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 {
答案 0 :(得分:1)
当然你做错了。
您应该将树对象提供给StringBuffer
函数,而不是Pipeline对象。要做到这一点 - 您必须从管道中获取树分类器并将其传递到export_graphviz
尝试使用这些最后一行来运行代码:
export_graphviz