如何从gridsearchcv绘制决策树? [英] how to plot a decision tree from gridsearchcv?

查看:446
本文介绍了如何从gridsearchcv绘制决策树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i was trying to plot the decision tree which is formed with GridSearchCV, but its giving me an Attribute error.

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

但是如果我尝试绘制普通的决策树

However if i try to plot a normal decision tree without GridSearchCv, then it successfully prints.

代码[没有gridsearchcv的决策树]

code [decision tree without 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的决策树]

code [decision tree with 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

错误

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的决策树代码

code for decision-tree based on 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中获取最佳参数,然后使用这些参数形成决策树并对其进行绘制。

One solution is taking the best parameters from gridsearchCV and then form a decision tree with those parameters and plot the tree.

但是有什么方法可以打印决策树

However is there any way to print the decision-tree based on GridSearchCV.

推荐答案

您可以尝试:

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

这篇关于如何从gridsearchcv绘制决策树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆