renerd66
renerd66

Reputation: 73

sklearn.tree.plot_tree show returns chunk of text instead of visualised tree

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!

Upvotes: 7

Views: 4805

Answers (2)

Mohammad Amin Dadgar
Mohammad Amin Dadgar

Reputation: 21

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

Upvotes: 0

Alex Serra Marrugat
Alex Serra Marrugat

Reputation: 2042

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

plot_tree(t)
plt.show()

Upvotes: 7

Related Questions