Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+9 votes

Is there a better way to view the search tree in sklearn. When you go further than a max_depth of 2 you cannot read what is inside the boxes.

asked in CSC320 by (1 point)

1 Answer

+3 votes
Best answer

The easiest option is to put these two lines of code before your plot_tree command.

import matplotlib.pyplot as plt

Another option (for a fancier tree look) is to export it to "graphviz" dot file format (using https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html), and then translate that .dot file into an actual graphic, using something that can open .dot files, such as the gvedit command within GraphViz (installed separately - https://www.graphviz.org/download/), or also I think Gephi (https://gephi.org/users/download/) might be able to import .dot files.

And, of course, you can export it as text... that should support any size, although it may be hard to decipher with all the levels of indentation.

answered by (508 points)
selected by

Thank you !!


Note: you can export the figure to a file, to make an arbitrarily large image:

plt.savefig("mytree.png", dpi=300)

You may want to play with the dpi (dots per inch) parameter, and also with the figure size -- (20,20) figure size until you get something you like.

Or if PNG doesn't work well, try exporting as PDF:


You might also play with adding the fontsize=8 (or some other size), keyword parameter into the plot_tree function.