dtreeviz

repository·master·Indexed 25 days ago

https://github.com/parrt/dtreeviz

A Python library for high-quality decision tree visualization and model interpretation. It supports major ML frameworks including scikit-learn, XGBoost, LightGBM, Spark MLlib, and TensorFlow (Decision Forests). Key features include tree structure visualization via .view(), prediction path explanations, leaf statistics, and decision boundary plotting for classifiers. It also offers AI-powered tree analysis through a .chat() method for interacting with tree architecture and node statistics.

Tokens
3.2K
Snippets
13
Records
18
Agent score
83%

What's inside dtreeviz

  1. Visualize decision tree models with dtreeviz

    master

    dtreeviz 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:

    1. Import dtreeviz and your ML library.
    2. Load your data.
    3. Train your model.
    4. Create a dtreeviz adaptor using dtreeviz.model().
    5. 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
  2. Configure Graphviz on Windows 10

    master

    On Windows, you must install the Graphviz binary and update your Path environment variable.

    1. Download the Graphviz MSI installer.
    2. Add C:\Program Files (x86)\Graphviz2.38\bin to your User path.
    3. Add C:\Program Files (x86)\Graphviz2.38\bin\dot.exe to your System Path.
    4. Verify the installation in an Anaconda Prompt using dot -V.

    Warning: Do not use conda install -c conda-forge python-graphviz as it provides an outdated version of the library.

    dot -V
  3. Install dtreeviz for local development

    master

    If you are developing on dtreeviz and need to run tests, install the library with the [dev] extra:

    pip install dtreeviz[dev]

    To force updates to your local egg cache during development (Windows/Anaconda Prompt):

    python setup.py install -f
    pip install dtreeviz[dev]
    python setup.py install -f
  4. Configure Graphviz on macOS

    master

    To use dtreeviz on macOS, you must have XCode and command-line tools installed. Run xcode-select --install and sign the license with sudo xcodebuild -license.

    You need the graphviz binary for dot. Install it via Homebrew:

    brew reinstall graphviz

    To avoid conflicts, ensure dot is removed from any Anaconda installation (e.g., rm ~/anaconda3/bin/dot). Verify the installation by running dot -Tsvg in the terminal; it should execute without error.

  5. Configure Graphviz on Linux (Ubuntu 18.04)

    master

    To get the dot binary on Ubuntu 18.04, run:

    sudo apt install graphviz

    Limitation: On this platform, the view() method works to pop up a new window and images appear inline for Jupyter Notebook, but not Jupyter Lab (which may encounter SVG XML parsing errors). Only .svg files can be generated.

  6. Install dtreeviz via pip

    master

    Install dtreeviz using pip. You can install specific extras depending on the machine learning library you are using. It is recommended to use an Anaconda Prompt on Windows.

    Note: Ensure you do not have conda-installed graphviz packages (like python-graphviz or graphviz) as dtreeviz requires the pip versions. You can remove them using:

    conda uninstall python-graphviz
    conda uninstall graphviz
    pip install dtreeviz             # install dtreeviz for sklearn
    pip install dtreeviz[xgboost]    # install XGBoost related dependency
    pip install dtreeviz[pyspark]    # install pyspark related dependency
    pip install dtreeviz[lightgbm]   # install LightGBM related dependency
    pip install dtreeviz[tensorflow_decision_forests]   # install tensorflow_decision_forests related dependency
    pip install dtreeviz[ai]         # install AI chat/explanation features (requires OpenAI API key)
    pip install dtreeviz[all]        # install all related dependencies
  7. Visualize classifier decision boundaries

    master

    The dtreeviz.decision_boundaries() utility function illustrates one and two-dimensional feature space for classifiers. It visualizes colors representing probabilities, decision boundaries, and misclassified entities.

    This method is not limited to tree models; it works with any model that implements a predict_proba() method (such as any scikit-learn classifier) or a predict() method (such as Keras models). Note that because this is a general utility, it does not use the dtreeviz adaptors obtained via dtreeviz.model().

  8. Use AI-powered tree analysis with chat()

    master

    If you enable AI integration, you can interact with your decision tree using the .chat() method. The AI can answer questions regarding:

    • Tree structure: Architecture, depth, node count, and splitting criteria.
    • Tree nodes: Split conditions, feature usage, and node statistics.
    • Leaf nodes: Predictions, confidence scores, and class distributions.
    • Training dataset: Feature statistics and data characteristics within nodes.

    When ai_chat=True is enabled, the standard .view() method will also automatically include an LLM-generated explanation alongside the visual output.

    Requirements:

    • Install with pip install dtreeviz[ai].
    • Set the OPENAI_API_KEY environment variable.
    # Enable AI chat when creating the model
    viz_model = dtreeviz.model(tree_classifier,
                               X_train=dataset[features], y_train=dataset[target],
                               feature_names=features,
                               target_name=target, class_names=["perish", "survive"],
                               ai_chat=True,
                               ai_model="gpt-4.1-mini",
                               max_history_messages=10)
    
    # Ask questions about your tree
    viz_model.chat("Please give me a short summary of the tree structure?")
    viz_model.chat("Which leaf nodes have the lowest prediction confidence?")
  9. Verify Graphviz installation

    master

    To ensure graphviz is correctly configured for dtreeviz, create a file named t.dot with the following content:

    digraph T { A -> B }

    Then run the following command in your terminal:

    dot -Tsvg -o t.svg t.dot

    If this generates a t.svg file that opens correctly, your installation is valid. If you receive errors from dot, dtreeviz will not function correctly.