簡體   English   中英

從管道繪制決策樹時出錯

[英]Error when plotting decision tree from pipeline

我正在嘗試繪制決策樹,但出現此錯誤:

'Pipeline' object has no attribute 'tree_' 

首先,我從預處理器(數據類型intobject )構建我的模型:

preprocessor = ColumnTransformer([
    ('one-hot-encoder', categorical_preprocessor, categorical_columns),
    ('standard_scaler', numerical_preprocessor, numerical_columns)])

model3 = make_pipeline(preprocessor, DecisionTreeClassifier())

我的管道

然后我擬合模型並生成預測:

model3 = model3.fit(data_train, target_train)
y_pred3 = model3.predict(data_test)

之后我嘗試繪制樹:

tree.plot_tree(model3)

但我得到了錯誤:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_22012/3111274197.py in <module>
----> 1 tree.plot_tree(model3)

~\anaconda3\lib\site-packages\sklearn\tree\_export.py in plot_tree(decision_tree, max_depth, feature_names, class_names, label, filled, impurity, node_ids, proportion, rounded, precision, ax, fontsize)
    193         fontsize=fontsize,
    194     )
--> 195     return exporter.export(decision_tree, ax=ax)
    196 
    197 

~\anaconda3\lib\site-packages\sklearn\tree\_export.py in export(self, decision_tree, ax)
    654         ax.clear()
    655         ax.set_axis_off()
--> 656         my_tree = self._make_tree(0, decision_tree.tree_, decision_tree.criterion)
    657         draw_tree = buchheim(my_tree)
    658 

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

如何繪制我的樹? 或者這是不可能的,因為我使用了管道?

如評論中所述,您應該訪問管道中的DecisionTreeClassifier實例以便能夠繪制樹,您可以執行以下操作:

plot_tree(model3.named_steps['decisiontreeclassifier'])

named_stepsPipeline的一個屬性,允許通過名稱訪問管道的步驟,而'decisiontreeclassifier'make_pipeline隱含的步驟名稱:

這是 Pipeline 構造函數的簡寫; 它不需要也不允許命名估算器。 相反,它們的名稱將自動設置為它們類型的小寫字母

暫無
暫無

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

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