scikit-learn/scikit-learn — pull request #3735 by trevorstephens (2014-10-04 05:41:59+00:00)
Title: [WIP] Pretty decision trees - ref #3643

As mentioned in #3643 this enhancement is intended to make the output of tree.export_graphviz more aesthetically pleasing as inspired by [fancyRpartPlot](http://blog.revolutionanalytics.com/2013/06/plotting-classification-and-regression-trees-with-plotrpart.html) in R. Colour brewing is done manually so the only additional import required is `numpy`, and output is still pure dot format.

Enhancements include:

- Drawing leaf nodes at the base of the tree for clarity
- Colours indicating how the node classification is 'leaning', including multi-class
- Percentage samples shown in node instead of raw number of samples
- Percentage per class shown for all nodes, not just leaves

Standard output, omitting the new `pretty` and `simple` flags, should result in the exact same graphical output from Graphviz as the incumbent version, though the dot code has been altered slightly for simplicity given the new more complicated node representation for `pretty`.

A few examples using the Titanic survivor dataset:

no flags:

![full_new](https://cloud.githubusercontent.com/assets/5210848/4514716/0a8c122a-4b88-11e4-92dd-78f78bbfcee9.png)

`pretty`:

![full_pretty](https://cloud.githubusercontent.com/assets/5210848/4514717/154df71e-4b88-11e4-9d09-b763ebe63763.png)

`pretty` & `simple`:

![full_pretty_simple](https://cloud.githubusercontent.com/assets/5210848/4514718/20512c9e-4b88-11e4-88f7-6eb78fabec4d.png)

`pretty` & `max_depth`:

![cropped_pretty](https://cloud.githubusercontent.com/assets/5210848/4514721/3957a330-4b88-11e4-9544-78f68cab3da5.png)

It has been moderately tested on pure `tree.DecisionTreeClassifier` objects, though I am well aware that regression needs work and will fail if attempted with the new flags (though the standard output still appears to be functional).

To be discussed:

- How to represent colours for regression... I'm thinking a diverging colour scheme with white for the median or mean of the two most extreme values in the leaves should work well.
- Should the text and graphical options be broken out further? Something like `show_major_class`, `use_colours`, `use_percentages`, etc., or some perhaps discarded. I based the options off the R package, that's not meant to imply that this is the state of the art, just the best I've stumbled across.
- Should the base implementation even be maintained? The extra sub-tree recursion per node is a little expensive, but it's just a single tree... And seeing what each node is voting towards gives more intuitive information than the error coefficients alone. It even appears that this used to be standard output from the examples [here](http://scikit-learn.org/stable/modules/tree.html#tree)

Other options that might be useful:

- Left to right orientation for use in reports?
- Anything else you can think of? Though Graphviz is a little limited in what you can pull off
- Welcome any other suggestions / gotchas