Search code examples
pythongraphvizxgboost

Visualize XGB using plot_tree on Macbook


I am trying to visualize the tree using xgb's built-in function "plot_tree" on a Macbook:

import numpy as np
from xgboost import XGBRegressor, plot_tree

X = np.random.randint(0, 100, 100).reshape(100, 1)
y = np.random.randint(0, 500, 100).reshape(100, 1)

model = XGBRegressor(n_estimators = 1)

model.fit(X, y)


plot_tree(model)

However, I get the following error:

ExecutableNotFound: failed to execute ['dot', '-Tpng'], make sure the Graphviz executables are on your systems' PATH

I tried with: 1. brew install graphviz 2. Set before at the beginning of the script

os.environ["PATH"] += os.pathsep + "/Users/alessandro/opt/anaconda3/bin/"

However, the do not seem to be working, reporting the same error.

How can I visualize the single tree I've trained using Graphviz or alternative solutions?


Solution

  • It seems that your dot command is located in the folder /usr/local/bin/.

    You can either add that folder to your PATH globally with the following terminal commands:

    sudo nano /etc/paths
    *type passsword*
    

    Then add the line /usr/local/bin to the file, or if it's already there, move it to the top of the list.

    To only add that folder to your PATH in this script I think you're command is very close, but needs slight adjustment:

    os.environ["PATH"] += os.pathsep + "/usr/local/bin/"
    

    Not the omission of dot as you're trying to add the folder, not the executable to the PATH.