如何从 gridsearchcv 绘制决策树?

Posted

技术标签:

【中文标题】如何从 gridsearchcv 绘制决策树?【英文标题】:how to plot a decision tree from gridsearchcv? 【发布时间】:2020-08-19 21:02:20 【问题描述】:

我试图绘制由 GridSearchCV 形成的决策树,但它给了我一个属性错误。

AttributeError: 'GridSearchCV' object has no attribute 'n_features_'

但是,如果我尝试在没有 GridSearchCv 的情况下绘制正常的决策树,那么它会成功打印。

代码[没有 gridsearchcv 的决策树]

# dtc_entropy : decison tree classifier based on entropy/information Gain
#plotting : decision tree on information/entropy  based

from sklearn.tree import export_graphviz
import graphviz

feature_names = x.columns

dot_data = export_graphviz(dtc_entropy, out_file=None, filled=True, rounded=True,
                                feature_names=feature_names,  
                                class_names=['0','1','2'])
graph = graphviz.Source(dot_data)  
graph                           ### --------------> WORKS 

代码 [带有 gridsearchcv 的决策树]

#plotting : decision tree with GRIDSEARCHCV (dtc_gscv)  on information/entropy  based
from sklearn.tree import export_graphviz
import graphviz

feature_names = x.columns

dot_data = export_graphviz(dtc_gscv, out_file=None, filled=True, rounded=True,
                                feature_names=feature_names,  
                                class_names=['0','1','2'])
graph = graphviz.Source(dot_data)  
graph                            ##### ------------> ERROR

错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-201-603524707f02> in <module>()
      6 dot_data = export_graphviz(dtc_gscv, out_file=None, filled=True, rounded=True,
      7                                 feature_names=feature_names,
----> 8                                 class_names=['0','1','2'])
      9 graph = graphviz.Source(dot_data)
     10 graph

1 frames
/usr/local/lib/python3.6/dist-packages/sklearn/tree/_export.py in export(self, decision_tree)
    393         # n_features_ in the decision_tree
    394         if self.feature_names is not None:
--> 395             if len(self.feature_names) != decision_tree.n_features_:
    396                 raise ValueError("Length of feature_names, %d "
    397                                  "does not match number of features, %d"

AttributeError: 'GridSearchCV' object has no attribute 'n_features_'

基于 GridSearchCV 的决策树代码

dtc=DecisionTreeClassifier()

#use gridsearch to test all values for n_neighbors
dtc_gscv = gsc(dtc, parameter_grid, cv=5,scoring='accuracy',n_jobs=-1)

#fit model to data
dtc_gscv.fit(x_train,y_train)

一个解决方案是从 gridsearchCV 中获取最佳参数,然后用这些参数形成决策树并绘制树。

但是有什么方法可以打印基于 GridSearchCV 的决策树。

【问题讨论】:

【参考方案1】:

你可以试试:

dot_data = export_graphviz(dtc_gscv.best_estimator_, out_file=None, 
            filled=True, rounded=True, feature_names=feature_names, class_names=['0','1','2'])

【讨论】:

@MAC,应该。 best_estmiator_ 用于分类和回归 但是类名应该是可选的。我正在尝试这个。 dot_data = tree.export_graphviz(model.best_estimator_.fit(X_train,y_train) , out_file=None, filled=True, rounded=True, feature_names=X_train.columns) graph = graphviz.Source(dot_data) graph ***.com/questions/62176516/…

以上是关于如何从 gridsearchcv 绘制决策树?的主要内容,如果未能解决你的问题,请参考以下文章

针对最大深度绘制决策树训练/测试精度

如何从 GridSearchCV 输出可视化 XGBoost 树?

如何在 R 中绘制/可视化 C50 决策树?

如何在网格搜索后绘制热图并找到决策树的最佳超参数

导出/绘制随机森林决策树/“RandomForestClassifier”对象没有属性“tree_”

如何在 Apache Spark (PySpark 1.4.1) 中可视化/绘制决策树?