簡體   English   中英

如何為 GradientBoostingClassifier 導出樹?

[英]How to export_tree for GradientBoostingClassifier?

此代碼適用於 DecisionTreeClassifier。

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))

但是,GradientBoostingClassifier 沒有用。

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?

您可以查看 GradientBoostingClassifier (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())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM