Skip to content

Add export_graphviz with plot_tree-style node panels - #92

Merged
upadhyan merged 4 commits into
mainfrom
feat/69-export-graphviz
Oct 17, 2026
Merged

upadhyan merged 4 commits into
mainfrom
feat/69-export-graphviz

Conversation

@upadhyan

Copy link
Copy Markdown
Collaborator

Closes #69.

What

Adds sgtlearn.export_graphviz(estimator, out_file=None, *, X=None, feature_names=None, class_names=None, filled=False, decimals=2), which returns (or writes) Graphviz DOT source for a fitted SGTClassifier / SGTRegressor.

Internal nodes draw the same panels as plot_tree, using Graphviz HTML-like labels, so the DOT is self-contained and graphviz stays out of the dependencies:

  • Univariate nodes: the shape function, shaded by the child each region routes to. With X, a histogram of the samples reaching the node, with bars coloured by child, plus a NaN column. Each colour switch gets a tick, and its threshold label is centred under it. Labels that would overlap move to another row.
  • Categorical nodes: one column per category bucket, labelled, plus a NaN column.
  • Pair (Shape虏CART) nodes: a routing heatmap with missing margins. With X, cells that contain samples are drawn in full colour, with gray marginal histograms. Continuous axes are ticked and labelled at each switch, x below and y to the left. This includes switches that only show in the other axis's missing margin.
  • Edges: coloured by child. Each edge's tooltip is the child's export_text rule, computed by the same _child_regions function, so the two exports can't disagree.
  • Leaves: show samples, value and class. filled=True shades leaves the way sklearn does.

Docs

  • export_graphviz is back in __all__, docs/api/index.rst and docs/api/plotting.rst.
  • The plotting docs include a rendered iris example, docs/_static/export_graphviz_iris.svg, and show how to render with graphviz.Source.
  • The ROADMAP item (both ROADMAP.md and docs/roadmap.rst) is ticked again.

Tests

In tests/test_export_text.py:

  • The DOT parses, and every panel label is well-formed XML.
  • There is one node per tree node, and edges match the children.
  • Edge tooltips match export_text lines for the univariate, categorical, pair-classifier and pair-regressor fixtures (K = 3 and K = 9).
  • Histograms appear only when X is given.
  • Ticks fall exactly on colour switches for univariate and pair axes, and threshold and NaN labels line up with their ticks and columns.
  • Input validation (estimator type, feature_names, X shape).

All 1424 tests pass. I rendered the output with viz.js (Graphviz compiled to WebAssembly) and it produced no Graphviz warnings. It hasn't been rendered with the native dot binary, which isn't installed on my machine.

Known limits

  • Continuous axes are drawn on a uniform grid (80 columns for univariate nodes, 24脳24 cells for pair heatmaps). Each threshold is therefore placed at the nearest cell, and a region narrower than one cell doesn't show. plot_tree uses exact interval widths.

Edge labels reuse the export_text routing rules, so both exports agree.
Nodes show samples, value and class; filled=True shades them like sklearn.
Closes #69.
Internal nodes now show their shape function shaded by destination child,
with an optional X histogram (pair nodes: routing heatmap with marginal
histograms). Edges are coloured by child and carry the export_text rule
as a tooltip instead of a label.
Each colour switch in a univariate panel now gets a tick, with its
threshold label centred under it (overlapping labels drop to a further
row), and the NaN label sits under the NaN column.
Continuous pair axes get a tick at each colour switch (including switches
visible only in the other axis's missing margin) with the threshold
label beside it: x below the heatmap, y to its left. Categorical axes
and missing margins are labelled per cell.
@upadhyan upadhyan self-assigned this Oct 17, 2026
@upadhyan
upadhyan merged commit 2be1648 into main Oct 17, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement export_graphviz (DOT export of shape-generalized trees)

1 participant