【发布时间】:2014-01-17 11:43:46
【问题描述】:
我正在尝试从 scikit-learn 集成中打印出森林的决策树:例如,对于 DecisionTreeClassifier,我会使用:
from sklearn import tree
clf = tree.DecisionTreeClassifier( criterion ='entropy', max_depth = 3,
min_samples_leaf =
clf = clf.fit( X_train, y_train) #Input this to analyze the training set.
import pydot, StringIO
dot_data = StringIO.StringIO()
tree.export_graphviz( clf, out_file = dot_data,
feature_names =[' age', 'sex', 'first_class', 'second_class', 'third_class'])
graph = pydot.graph_from_dot_data( dot_data.getvalue())
graph.write_png('visualtree.png')
from IPython.core.display import Image
Image( filename =visualtree.png')
我为随机森林回归器尝试了类似的方法(见下文并出现错误)
# Fit regression model
from sklearn.ensemble import RandomForestRegressor
rfr_1 = RandomForestRegressor(n_estimators=10, max_depth=5)
rfr_1.fit(X, y)
from sklearn.ensemble import*
import pydot, StringIO
dot_data = StringIO.StringIO()
ensemble.export_graphviz( rf1, out_file = dot_data,
feature_names =[' Temperature', 'Translator Bacteria'])
graph = pydot.graph_from_dot_data( dot_data.getvalue())
graph.write_png('fish.png')
from IPython.core.display import Image
图片(文件名='fish.png')
文件“randomforestregressor.py”,第 45 行,在
ensemble.export_graphviz(rf1, out_file = dot_data,
NameError: name 'ensemble' 未定义
我将如何做到这一点?谢谢!
【问题讨论】:
标签: python numpy matplotlib graphviz scikit-learn