如何为梯度bloostingClassifier导出_tree?

发布于 2025-02-03 20:25:32 字数 1417 浏览 2 评论 0原文

此代码适用于决策仪。

r = export_text(tree2, feature_names=fn)
print(r)

但是,对于RandomForestClassifier

from sklearn.tree import export_text

print(export_text(tree3.estimators_[0], 
                  spacing=3, decimals=3,
                  feature_names=fn))

,渐变BoostingClassifier无法使用。

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1840/2106124489.py in <module>
      1 from sklearn.tree import export_text
----> 2 r = export_text(tree4, feature_names=fn)
      3 print(r)

~\anaconda\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~\anaconda\anaconda3\lib\site-packages\sklearn\tree\_export.py in export_text(decision_tree, feature_names, max_depth, spacing, decimals, show_weights)
    875     """
    876     check_is_fitted(decision_tree)
--> 877     tree_ = decision_tree.tree_
    878     if is_classifier(decision_tree):
    879         class_names = decision_tree.classes_

AttributeError: 'GradientBoostingClassifier' object has no attribute 'tree_'

有没有办法在GradientBoostingClassifier中显示Export_Tree?

This code works for DecisionTreeClassifier.

r = export_text(tree2, feature_names=fn)
print(r)

And for RandomForestClassifier

from sklearn.tree import export_text

print(export_text(tree3.estimators_[0], 
                  spacing=3, decimals=3,
                  feature_names=fn))

However, GradientBoostingClassifier didn't work.

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1840/2106124489.py in <module>
      1 from sklearn.tree import export_text
----> 2 r = export_text(tree4, feature_names=fn)
      3 print(r)

~\anaconda\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~\anaconda\anaconda3\lib\site-packages\sklearn\tree\_export.py in export_text(decision_tree, feature_names, max_depth, spacing, decimals, show_weights)
    875     """
    876     check_is_fitted(decision_tree)
--> 877     tree_ = decision_tree.tree_
    878     if is_classifier(decision_tree):
    879         class_names = decision_tree.classes_

AttributeError: 'GradientBoostingClassifier' object has no attribute 'tree_'

Is there a way to show the export_tree in GradientBoostingClassifier?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2025-02-10 20:25:32

您可以查看渐变bloostingClassifier(GBC)的基础决策树,而不是GBC本身。

假设您的GBC模型是MDL

mdl = GradientBoostingClassifier(n_estimators=100, max_depth=5)

您可以选择一棵树并查看

from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz
from IPython.display import Image

gbc_sub_tree = mdl.estimators_[10, 0]

graph_data = export_graphviz(gbc_sub_tree, out_file=None, rounded=True, proportion=False, impurity=False)
tree_graph = graph_from_dot_data(graph_data)
Image(tree_graph.create_png())

You can view the underlying decision tree of a GradientBoostingClassifier (GBC), not the GBC itself.

Assuming your GBC model is mdl

mdl = GradientBoostingClassifier(n_estimators=100, max_depth=5)

You can select a tree and view it

from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz
from IPython.display import Image

gbc_sub_tree = mdl.estimators_[10, 0]

graph_data = export_graphviz(gbc_sub_tree, out_file=None, rounded=True, proportion=False, impurity=False)
tree_graph = graph_from_dot_data(graph_data)
Image(tree_graph.create_png())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文