![](/img/trans.png)
[英]Error: “TypeError: 'module' object is not callable” when plotting tree for decision tree model
[英]Error when plotting decision tree from pipeline
我正在嘗試繪制決策樹,但出現此錯誤:
'Pipeline' object has no attribute 'tree_'
首先,我從預處理器(數據類型int
和object
)構建我的模型:
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_steps
是Pipeline
的一個屬性,允許通過名稱訪問管道的步驟,而'decisiontreeclassifier'
是make_pipeline
隱含的步驟名稱:
這是 Pipeline 構造函數的簡寫; 它不需要也不允許命名估算器。 相反,它們的名稱將自動設置為它們類型的小寫字母。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.