Implementation:Scikit learn Scikit learn ExportTree
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Visualization |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for exporting and visualizing decision trees in various formats provided by scikit-learn.
Description
This module defines export functions for decision trees, enabling visualization in Graphviz DOT format and plain text format. The export_graphviz function produces DOT language output suitable for rendering with Graphviz tools, supporting colored nodes, feature/class names, and impurity annotations. The export_text function generates a human-readable text representation of the decision tree rules. The module also includes internal helper classes (_DOTTreeExporter, _MPLTreeExporter) for different rendering backends.
Usage
Use export_graphviz to create publication-quality tree visualizations via Graphviz, or export_text for quick text-based inspection of tree decision rules in notebooks or command-line output.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/tree/_export.py
Signature
@validate_params(...)
def export_graphviz(
decision_tree,
out_file=None,
*,
max_depth=None,
feature_names=None,
class_names=None,
label="all",
filled=False,
leaves_parallel=False,
impurity=True,
node_ids=False,
proportion=False,
rotate=False,
rounded=False,
special_characters=False,
precision=3,
fontname="helvetica",
)
@validate_params(...)
def export_text(
decision_tree,
*,
feature_names=None,
class_names=None,
max_depth=10,
spacing=3,
decimals=2,
show_weights=False,
)
Import
from sklearn.tree import export_graphviz, export_text
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| decision_tree | decision tree estimator | Yes | Fitted decision tree classifier or regressor |
| out_file | file object or str or None | No | Output file for Graphviz DOT data (default: None returns string) |
| max_depth | int or None | No | Maximum depth of tree to export (default: None for full tree) |
| feature_names | list of str or None | No | Feature names for node labels (default: None) |
| class_names | list of str or bool or None | No | Class names for leaf labels (default: None) |
| filled | bool | No | Color nodes by majority class or value (default: False) |
| rounded | bool | No | Use rounded node boxes in Graphviz (default: False) |
| precision | int | No | Number of decimal places for values (default: 3) |
| spacing | int | No | Indentation spacing for export_text (default: 3) |
Outputs
| Name | Type | Description |
|---|---|---|
| dot_data | str | Graphviz DOT format string (export_graphviz) |
| text_report | str | Text representation of tree rules (export_text) |
Usage Examples
Basic Usage
from sklearn.tree import DecisionTreeClassifier, export_text, export_graphviz
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
clf = DecisionTreeClassifier(max_depth=3, random_state=42)
clf.fit(X, y)
# Text export
print(export_text(clf, feature_names=load_iris().feature_names))
# Graphviz export
dot_data = export_graphviz(clf, feature_names=load_iris().feature_names,
class_names=load_iris().target_names,
filled=True, rounded=True)