A. Follow this method for plotting using Graphviz:
1. First, install the libraries required to plot the decision tree. Install all these libraries using Anaconda Prompt, and not directly from the notebook.
To open Anaconda Prompt, go to Start menu and type Anaconda Prompt, and in the search results, select Anaconda Prompt and run it.
If these libraries are not already installed in your system. Install them using these commands.
Installing Pydotplus and Pyparsing.
!pip install pydotplus
!pip install pyparsing
!conda install python-graphviz
2. Plotting the decision tree in the notebook.
from pydotplus import graph_from_dot_data
from sklearn.tree import export_graphviz
Specifying the location of graphviz in your system temporarily for this notebook.
Otherwise, you'll see the error: GraphViz's Executables not found
os.environ['PATH'] = os.environ['PATH'] + ';' + os.environ['CONDA_PREFIX'] + r"\Library\bin\graphviz"
Creating the decision tree plot
dot_data = export_graphviz(regressor_dt, filled=True, rounded=True, feature_names=X.columns, out_file=None)
graph = graph_from_dot_data(dot_data)
Saving the decision tree plot
The Decision Tree plot will be saved as tree.png in the same directory as the notebook.
It can be accessed from there.
In the above code, regressor_dt used in export_graphviz is the name of the decision tree model. You can replace it with the name of your model.
B. If you want to avoid Graphviz installation then follow this process
from sklearn.tree import plot_tree
plot_tree(model, feature_names=X.columns, filled=True, rounded=True)