A Rob4 A Rob4 - 1 month ago 13
Python Question

'GridSearchCV' object has no attribute 'tree_' error using graphviz

I'm using a gridsearchCV to set parameters for a decision tree regressor as below.

clf = GridSearchCV(DecisionTreeRegressor(random_state=99),parameters,refit=True,cv=5) # default is MSE
clf.fit(x_train, y_train)


I then want to pass this output a chart using Graphviz

export_graphviz(clf, out_file='tree.dot',feature_names=df_temp.columns,leaves_parallel=True)
subprocess.call(['dot', '-Tpdf', 'tree.dot', '-o' 'tree.pdf'])


I'm getting an error message however:
'GridSearchCV' object has no attribute 'tree_'

I'm really stuck with this and could do with some help please!

Answer

export_graphviz expects DecisionTreeRegressor, not GridSearchCV; try using export_graphviz(clf.best_estimator_, ...).

Comments