Scikit learn 使用Sklearn'时出现未安装错误;s graphviz

Scikit learn 使用Sklearn'时出现未安装错误;s graphviz,scikit-learn,Scikit Learn,当我尝试使用以下命令导出随机林图时: tree.export_graphviz(rnd_clf, out_file = None, feature_names = X_test[::1]) 我收到以下错误: NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method. 我不明白的是,

当我尝试使用以下命令导出随机林图时:

tree.export_graphviz(rnd_clf, out_file = None, feature_names = X_test[::1])
我收到以下错误:

NotFittedError: This RandomForestClassifier instance is not fitted yet. 
Call 'fit' with appropriate arguments before using this method.
我不明白的是,为什么它一直告诉我这些,尽管我使用以下方法安装了随机森林分类器:

rnd_clf = RandomForestClassifier(  
             n_estimators=120,
             criterion='gini',
             max_features= None, 
             max_depth = 14 )

rnd_clf.fit(X_train, y_train)
而且它工作得非常好。

(只看文档;没有个人经验)

您正试图使用一个函数绘制一些决策树,该函数的签名如下:

sklearn.tree.export_graphviz(decision_tree, ...)
但是您经过的是一个随机森林,它是一个树群

那是行不通的

更深入地说,这方面的内部代码是:

因此,这是询问您的决策树的属性
,它存在于

此属性不存在于!因此,这是一个错误


你能做的唯一一件事是:打印你的RandomForest集合中的每个决策树。为此,您需要遍历
random_forest.estimators_
以获得底层决策树

就像另一个答案所说的,你不能为一片森林做这件事,只有一棵树。但是,您可以绘制该森林中的一棵树的图形。以下是如何做到这一点:

forest_clf = RandomForestClassifier()
forest_clf.fit(X_train, y_train)
tree.export_graphviz(forest_clf.estimators_[0], out_file='tree_from_forest.dot')
(graph,) = pydot.graph_from_dot_file('tree_from_forest.dot')
graph.write_png('tree_from_forest.png')

不幸的是,没有简单的方法可以从您的森林中绘制“最佳”树或整体集成树,只是一个随机示例树。

谢谢!我只是认为export_graphviz(决策树,…)能够显示完整的随机林。一个快速的后续问题是,在我的例子中显示了100多棵树,这会带来一个可视化问题。是否可以创建一个表格,在每个树深度显示最具区别的特征?那不是我的专长。很抱歉
enter code herefrom IPython.display import Image
from sklearn.`enter code here`externals.six import StringIO
from sklear`enter code here`n.tree import export_graphviz
import pydotplus
import pydot`enter code here`

    dt = DecisionTreeClassifier(criterion = 'entropy', max_depth = 3, min_samples_split = 20, class_weight = "balanced")
    dtree = dt.fit(ctg_x_train,ctg_y_train)
    
        k
    
    dot_data = StringIO()
    ctg_x_train_names = ctg_x_train.columns
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize = (12,12))
    
    export_graphviz(dtree, out_file=dot_data,filled = True, rounded = True,special_characters = True, feature_names = ctg_x_train_names)
    
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    
    (graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
    Image(graph.create_png())
enter code herefrom IPython.display import Image
from sklearn.`enter code here`externals.six import StringIO
from sklear`enter code here`n.tree import export_graphviz
import pydotplus
import pydot`enter code here`

    dt = DecisionTreeClassifier(criterion = 'entropy', max_depth = 3, min_samples_split = 20, class_weight = "balanced")
    dtree = dt.fit(ctg_x_train,ctg_y_train)
    
        k
    
    dot_data = StringIO()
    ctg_x_train_names = ctg_x_train.columns
    import matplotlib.pyplot as plt
    fig = plt.figure(figsize = (12,12))
    
    export_graphviz(dtree, out_file=dot_data,filled = True, rounded = True,special_characters = True, feature_names = ctg_x_train_names)
    
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
    
    (graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
    Image(graph.create_png())