7

I'm trying to show a tree visualisation using plot_tree, but it shows a chunk of text instead:

from sklearn.tree import plot_tree
plot_tree(t)

(where t is an instance of DecisionTreeClassifier) This is the output:

[Text(464.99999999999994, 831.6, 'X[3] <= 0.8\nentropy = 1.581\nsamples = 120\nvalue = [39, 37, 44]'),
 Text(393.46153846153845, 646.8, 'entropy = 0.0\nsamples = 39\nvalue = [39, 0, 0]'),

and so on and so forth. How do I make it show the visual tree instead? I'm using Jupyter 6.4.1 and I already imported matplotlib earlier in the code. Thanks!

2 Answers 2

7

In my case, it works with a simple "show":

plot_tree(t)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You can plot your tree and specify the plot size of your tree with plt.figure

width = 10
height = 7
plt.figure(figsize=(width, height))

tree_plot_max_depth = 6
plot_tree(t, max_depth=tree_plot_max_depth)

## the key to the problem of not showing tree is the command below
plt.show()

And as the documentation is mentioned below you can specify more parameters for your tree to get a more informative image.

https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.