Visualizing Decision Tree Not Using Graphviz/web
Due to some restriction I cannot use graphviz , webgraphviz.com to visualize decision tree (work network is closed from the other world). Question: Is there some alternative util
Solution 1:
Here is an answer that doesn't use either graphviz or an online converter. As of scikit-learn version 21.0 (roughly May 2019), Decision Trees can now be plotted with matplotlib using scikit-learn’s tree.plot_tree without relying on graphviz.
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
X, y = load_iris(return_X_y=True)
# Make an instance of the Model
clf = DecisionTreeClassifier(max_depth = 5)
# Train the model on the data
clf.fit(X, y)
fn=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
cn=['setosa', 'versicolor', 'virginica']
# Setting dpi = 300 to make image clearer than default
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=300)
tree.plot_tree(clf,
feature_names = fn,
class_names=cn,
filled = True);
fig.savefig('imagename.png')
The image below is what is saved.
The code was adapted from this post.
Post a Comment for "Visualizing Decision Tree Not Using Graphviz/web"