【问题标题】:NotFittedError: This BalancedRandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this methodNotFittedError:尚未安装此 BalancedRandomForestClassifier 实例。在使用此方法之前使用适当的参数调用“fit”
【发布时间】:2020-04-14 11:52:25
【问题描述】:

我很想绘制我的模型,但代码是错误的,该模型尚未安装。但我安装了模型。有人可以帮我解释为什么我会收到这个错误吗?

我的代码如下;

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.

【问题讨论】:

  • 你能发布完整的回溯错误吗?还有一个最小的可重现错误。
  • 我添加了完整的错误文本。
  • 我会回滚我的答案......因为@glemaitre 的答案比我的更准确。

标签: plot graph scikit-learn model stringio


【解决方案1】:

export_graphviz 需要单个树模型。因此,当这是一个整体时,您需要遍历树。 BalancedRandomForestClassifier 暴露 estimators_ 用于此类用途。正如 Parthasarathy Subburaj 所说,您可以循环其他 estimators_ 并调用该函数。

这就是说我建议你使用sklearn.tree.plot_tree(...):https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html

它是一个纯 matplotlib 绘图助手,如果您只想获得图像表示而不使用 graphviz,它会更容易

【讨论】:

  • 在这种方法中,当我使用 DecisionTreeClassifier 时,它起作用了。但是当我使用 BalancedRandomForestClassifier 时,出现如下错误; AttributeError: 'BalancedRandomForestClassifier' 对象没有属性 'tree_' 此方法仅用于 DecisionTreeClassifier ??
  • 正如BalancedRandomForestClassifierRandomForestClassifier 等解释的那样,是一组预测变量。这意味着它们由几个预测变量组成。这些估计器存储在model.estimators_ 中。因此model.estimators_[0] 将是组合中的第一个DecisionTreeClassifier,您可以使用export_graphviz 显示它。然后,您将拥有与 model.estimators_ 中的估计器数量一样多的地块(因此您的集合中的树数)。
猜你喜欢
  • 2022-11-07
  • 2019-10-05
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多