Skip to content Skip to sidebar Skip to footer

Plot Best Decision Tree With Pipeline And Gridsearchcv

I have GridsearchCV with a pipeline using decision tree as estimator Now i want to plot the decision tree corresponding to the best_estimator of the GridsearchCV There are some rep

Solution 1:

Since your estimators are Pipeline objects, the best_estimator_ attribute will return a pipeline as well. You have to further access the correct step with your regressor by indexing it, for example:

plot_tree(
    Dtree.best_estimator_['regressor'],  # <-- added indexing here
    max_depth=5,
    impurity=True,
    feature_names=['X1', 'X2', 'X3', 'X4'],   # changed this argument to make it work properly
    precision=1,
    filled=True
)

See the user guide on different methods to access pipeline steps.

In case you are wondering why your error message says the pipeline is not fitted, you can read more about it in my answer here.

Solution 2:

I found the solution:

I have to use Dtree.best_estimator_['regressor'] instead of Dtree.best_estimator:

plot_tree(Dtree.best_estimator_, max_depth=5,
          impurity=True,
          feature_names=('X'),
          precision=1, filled=True)

Post a Comment for "Plot Best Decision Tree With Pipeline And Gridsearchcv"