![](/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.