liveBook · Manning

Plot_tree

As explained in Statistics Every Programmer Needs

plot_tree is a scikit-learn function that renders a fitted decision tree as a visual tree diagram.

plot_tree renders a fitted scikit-learn decision tree as a visual diagram. Feature names and class names label the nodes, while node colors indicate the majority or predicted class. section 6.3.5

Useful display options include:

  • filled=True colors nodes according to their majority class or predicted class and split purity. section 6.3.5
  • rounded=True rounds node edges, improving visual presentation. section 6.3.5
  • fontsize controls label readability; feature_names and class_names identify variables and outcomes. section 6.3.5

Plot a pruned classifier

Pass a fitted classifier to tree.plot_tree with feature_names such as QUARTER, TO_GO, OFFENSIVE_TEAM_VENUE, SCORE_DIFFERENTIAL, and PLAY_TYPE, then display the Matplotlib figure. A pruned tree contains fewer splits and features, making its decision paths easier to inspect. Thresholds can be non-integer, such as TO_GO <= 3.5, even when the feature is integer-valued; this does not affect predictive accuracy. section 6.3.5

>>> from sklearn import tree
>>> plt.figure()
>>> tree.plot_tree(clf, feature_names = ['QUARTER',
>>>                                      'TO_GO',
>>>                                      'OFFENSIVE_TEAM_VENUE',
>>>                                      'SCORE_DIFFERENTIAL',
>>>                                      'PLAY_TYPE'],
>>>                class_names = ['0', '1'],
>>>                filled = True,
>>>                rounded = True
>>>                fontsize = 12)
>>> plt.tight_layout()
>>> plt.show()

Use the runnable listing to see the scikit-learn import, label configuration, visual styling, and Matplotlib display call. section 6.3.5

How Statistics Every Programmer Needs covers it

How deep it goes
Explains display parameters and threshold appearance, but defers detailed interpretation of impurity, pruning criteria, and tree mechanics.
Examples
Uses a pruned football classifier with QUARTER, TO_GO, venue, score differential, and play type features.
What it assumes
Assumes a fitted decision-tree classifier and names for its features and classes.
What you can do after
Apply it via scikit-learn and Matplotlib to generate and inspect a labeled decision-tree diagram.
How it connects
Connects decision trees with pruning, feature names, class names, impurity, and Matplotlib visualization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.