Visualize decision tree models with dtreeviz
masterdtreeviz is a Python library for decision tree visualization and model interpretation. It supports several machine learning libraries including scikit-learn, XGBoost, Spark MLlib, LightGBM, and TensorFlow (Decision Forests).
To use dtreeviz, you follow a standard workflow:
- Import
dtreevizand your ML library. - Load your data.
- Train your model.
- Create a dtreeviz adaptor using
dtreeviz.model(). - Use the adaptor to call visualization methods like
.view()or.explain_prediction_path().
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
import dtreeviz
iris = load_iris()
X = iris.data
y = iris.target
clf = DecisionTreeClassifier(max_depth=4)
clf.fit(X, y)
viz_model = dtreeviz.model(clf,
X_train=X, y_train=y,
feature_names=iris.feature_names,
target_name='iris',
class_names=iris.target_names)
v = viz_model.view() # render as SVG into internal object
v.show() # pop up window
v.save("/tmp/iris.svg") # optionally save as svg