'Display this decision tree with Graphviz
I am following a tutorial on using python v3.6 to do decision tree with machine learning using scikit-learn.
Here is the code;
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mglearn
import graphviz
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
tree = DecisionTreeClassifier(max_depth=4, random_state=0)
tree.fit(X_train, y_train)
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file="tree.dot", class_names=["malignant", "benign"],feature_names=cancer.feature_names, impurity=False, filled=True)
import graphviz
with open("tree.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
How do I use Graphviz to see what is inside dot_graph? Presumably, it should look something like this;
Solution 1:[1]
In jupyter notebook the following plots the decision tree:
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
model = DecisionTreeClassifier()
model.fit(X, y)
dot_data = tree.export_graphviz(model,
feature_names=feature_names,
class_names=class_names,
filled=True, rounded=True,
special_characters=True,
out_file=None,
)
graph = graphviz.Source(dot_data)
graph
if you want to save it as png:
graph.format = "png"
graph.render("file_name")
Solution 2:[2]
You can use display from IPython.display. Here is an example:
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
model = DecisionTreeClassifier()
model.fit(X, y)
from IPython.display import display
display(graphviz.Source(tree.export_graphviz(model)))
Solution 3:[3]
I'm working in Windows 10. I solved this by adding to the 'path' environment variable. I added the wrong path, I added Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\lib\site-packages\graphviz should have used Drive:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\Library\bin\graphviz in the end I used both, then restarted python/anaconda. Also added the pydotplus path, which is in ....MyVirtualEnv\lib\site-packages\pydotplus.
Solution 4:[4]
Jupyter will show the graph as is, but if you want to zoom in more you can try to save the file and inspect further :
# Draw graph
graph = pydotplus.graph_from_dot_data(dot_data)
# Show graph
Image(graph.create_png())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ttfreeman |
| Solution 3 | Vince Hall |
| Solution 4 | Suraj Rao |

