Plot NotFitteError:此BalancedAndomForestClassifier实例尚未安装。呼叫';适合';在使用此方法之前使用适当的参数

Plot NotFitteError:此BalancedAndomForestClassifier实例尚未安装。呼叫';适合';在使用此方法之前使用适当的参数,plot,graph,scikit-learn,model,stringio,Plot,Graph,Scikit Learn,Model,Stringio,我正试图绘制我的模型,但代码是错误的,模型还没有拟合。但我适合这个模型。有人能帮我解释为什么我会犯这个错误吗 我的代码如下 model = BalancedRandomForestClassifier(n_estimators = 200, random_state = 0, max_depth=6) model.fit(x_train, y_train) y_pred_rf = model.predict(x_test) dot_data = StringIO() export_graph

我正试图绘制我的模型,但代码是错误的,模型还没有拟合。但我适合这个模型。有人能帮我解释为什么我会犯这个错误吗

我的代码如下

model = BalancedRandomForestClassifier(n_estimators = 200, random_state = 0, max_depth=6)

model.fit(x_train, y_train)
y_pred_rf = model.predict(x_test)

dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
            filled=True, rounded=True,
            special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())
---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
<ipython-input-57-0036434b9b2c> in <module>
 16 export_graphviz(model, out_file=dot_data,  
 17             filled=True, rounded=True,
 ---> 18             special_characters=True)
 19 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
 20 Image(graph.create_png())

 /opt/anaconda/envs/env_python/lib/python3.6/site- 
 packages/sklearn/tree/export.py in export_graphviz(decision_tree, out_file, 
 max_depth, feature_names, class_names, label, filled, leaves_parallel, 
 impurity, node_ids, proportion, rotate, rounded, special_characters, 
 precision)
 754     """
 755 
 --> 756     check_is_fitted(decision_tree, 'tree_')
 757     own_file = False
 758     return_string = False

 /opt/anaconda/envs/env_python/lib/python3.6/site- 
 packages/sklearn/utils/validation.py in check_is_fitted(estimator, 
 attributes, msg, all_or_any)
 912 
 913     if not all_or_any([hasattr(estimator, attr) for attr in 
 attributes]):
 --> 914         raise NotFittedError(msg % {'name': 
 type(estimator).__name__})
 915 
 916 

 NotFittedError: This BalancedRandomForestClassifier instance is not fitted 
 yet. Call 'fit' with appropriate arguments before using this method.
误差如下

model = BalancedRandomForestClassifier(n_estimators = 200, random_state = 0, max_depth=6)

model.fit(x_train, y_train)
y_pred_rf = model.predict(x_test)

dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
            filled=True, rounded=True,
            special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())
---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
<ipython-input-57-0036434b9b2c> in <module>
 16 export_graphviz(model, out_file=dot_data,  
 17             filled=True, rounded=True,
 ---> 18             special_characters=True)
 19 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
 20 Image(graph.create_png())

 /opt/anaconda/envs/env_python/lib/python3.6/site- 
 packages/sklearn/tree/export.py in export_graphviz(decision_tree, out_file, 
 max_depth, feature_names, class_names, label, filled, leaves_parallel, 
 impurity, node_ids, proportion, rotate, rounded, special_characters, 
 precision)
 754     """
 755 
 --> 756     check_is_fitted(decision_tree, 'tree_')
 757     own_file = False
 758     return_string = False

 /opt/anaconda/envs/env_python/lib/python3.6/site- 
 packages/sklearn/utils/validation.py in check_is_fitted(estimator, 
 attributes, msg, all_or_any)
 912 
 913     if not all_or_any([hasattr(estimator, attr) for attr in 
 attributes]):
 --> 914         raise NotFittedError(msg % {'name': 
 type(estimator).__name__})
 915 
 916 

 NotFittedError: This BalancedRandomForestClassifier instance is not fitted 
 yet. Call 'fit' with appropriate arguments before using this method.
---------------------------------------------------------------------------
NotFittedError回溯(最近一次呼叫最后一次)
在里面
16导出图形(模型,输出文件=点数据,
17填充=真,四舍五入=真,
--->18个特殊字符=真)
19 graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
20图像(graph.create_png())
/opt/anaconda/envs/env_python/lib/python3.6/site-
export_graphviz中的packages/sklearn/tree/export.py(决策树、输出文件、,
最大深度、特征名称、类别名称、标签、填充、保持平行、,
杂质、节点ID、比例、旋转、圆角、特殊字符、,
精度)
754     """
755
-->756检查是否安装(决策树“树”)
757 own_file=False
758返回_字符串=False
/opt/anaconda/envs/env_python/lib/python3.6/site-
检查中的packages/sklearn/utils/validation.py已安装(估计器,
属性、消息、全部或任何)
912
913如果不是所有的或任何([hasattr(estimator,attr))中的attr
属性]:
-->914 raise NOTFITTEDEError(消息%{'name':
类型(估计器)。\uuuuu名称\uuuu})
915
916
NotFittedError:此BalancedAndomForestClassifier实例未安装
但是,在使用此方法之前,请使用适当的参数调用“fit”。

export\u graphviz
需要一个单一的树模型。因此,当这是一个集合时,您需要在树上循环。
BalancedAndomForestClassifier
为这种用法公开了
estimators\uu
。如Parthasarathy Subburaj所述,您可以循环其他
estimators\uu
并调用该函数

我建议您使用
sklearn.tree.plot\u tree(…)


这是一个纯粹的matplotlib绘图辅助工具,如果您只想获得图像表示而不使用
graphviz

可以发布完整的回溯错误吗?以及最小的可复制错误。我添加了完整的错误文本。我将回滚我的答案…因为@glemaitre答案比我的更准确。在这种方法中n我使用DecisionTreeClassifier,它起作用了。但当我使用BalancedAndomForestClassifier时,我得到了如下错误;AttributeError:“BalancedAndomForestClassifier”对象没有属性“tree”\u此方法仅用于DecisionTreeClassifier??如
BalancedAndomForestClassifier
RandomForestClassifier
所述,etc是预测值的集合。这意味着它们由多个预测值组成。这些估计值存储在
模型中。估计值\uu
。因此
模型。估计值\u0]
将是集合中的第一个
DecisionTreeClassifier
,您可以使用
export\u graphviz
显示它。然后,您将拥有与
模型中估计器数量相同的图。估计器
(以及集合中的树数)。