使用 Sklearn 的 graphviz 时未拟合错误 [英] Not Fitted Error when using Sklearn's graphviz
问题描述
当我尝试使用以下命令导出随机森林图时:
when I am trying to export a random forest graph using the following command:
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.
我不明白的是为什么它一直告诉我这个,即使我已经使用以下方法安装了随机森林分类器:
What I don't understand is why it keeps telling me this, even though I have fitted the random forest classifier using:
rnd_clf = RandomForestClassifier(
n_estimators=120,
criterion='gini',
max_features= None,
max_depth = 14 )
rnd_clf.fit(X_train, y_train)
而且它工作得很好.
推荐答案
(只看文档;没有亲身经历)
(Only going by the docs; no personal experience)
您正在尝试使用签名读取的函数绘制一些决策树:
You are trying to plot some DecisionTree, using a function which signature reads:
sklearn.tree.export_graphviz(decision_tree, ...)
但是您正在传递一个RandomForest,它是一个树的集合.
but you are passing a RandomForest, which is an ensemble of trees.
这行不通!
再深入一点,内部代码是此处:
Going deeper, the code internally for this is here:
check_is_fitted(decision_tree, 'tree_')
所以这是要求你的 DecisionTree 的属性 tree_
,它存在于 DecisionTreeClassifier.
So this is asking for the attribute tree_
of your DecisionTree, which exists for a DecisionTreeClassifier.
RandomForestClassifier 不存在此属性!因此错误.
This attribute does not exist for a RandomForestClassifier! Therefore the error.
您唯一能做的就是:打印 RandomForest 集成中的每个决策树.为此,您需要遍历 random_forest.estimators_
以获取底层决策树!
The only thing you can do: print every DecisionTree within your RandomForest ensemble. For this, you need to traverse random_forest.estimators_
to get the underlying decision-trees!
这篇关于使用 Sklearn 的 graphviz 时未拟合错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!