Search code examples
pythongraphvizxgboost

Plotting tree with XGBoost returns Graphviz error


I've tried to plot decision tree from XGBoost using Plot a Single XGBoost Decision Tree. and https://machinelearningmastery.com/visualize-gradient-boosting-decision-trees-xgboost-python/.
My code:

import matplotlib.pyplot as plt
import xgboost as xgb

model = xgb.XGBClassifier()
model.load_model("./models/acute_inflammations.model")
#res = model.predict_proba(X_test.iloc[0:1])
xgb.plot_tree(model)
plt.show()

Model was saved with save_model method and is working otherwise (I've tried predicting with it). The only problem is with plot_tree function, which returns the following error:

Format: "png" not recognized. Use one of:
Traceback (most recent call last):
  File "C:/Users/jakub/Desktop/thesis_ML/main.py", line 35, in <module>
    xgb.plot_tree(model)
  File "C:\Users\jakub\anaconda3\envs\Inz_ML\lib\site-packages\xgboost\plotting.py", line 248, in plot_tree
    s.write(g.pipe(format='png'))
  File "C:\Users\jakub\anaconda3\envs\Inz_ML\lib\site-packages\graphviz\files.py", line 136, in pipe
    out = backend.pipe(self._engine, format, data,
  File "C:\Users\jakub\anaconda3\envs\Inz_ML\lib\site-packages\graphviz\backend.py", line 244, in pipe
    out, _ = run(cmd, input=data, capture_output=True, check=True, quiet=quiet)
  File "C:\Users\jakub\anaconda3\envs\Inz_ML\lib\site-packages\graphviz\backend.py", line 183, in run
    raise CalledProcessError(proc.returncode, cmd,
graphviz.backend.CalledProcessError: Command '['dot', '-Tpng']' returned non-zero exit status 1. [stderr: b'Format: "png" not recognized. Use one of:\r\n']

Process finished with exit code 1

I have Graphviz installed both in Windows ("Stable 2.44 Windows install packages" from the Downloads page) and in Python (pip install graphviz). Graphviz is in my PATH system variable. What am I doing wrong? How can I fix this? Can I even fix this, or is it something in XGBoost internals changed with recent Graphviz update (they've updated recently)?


Solution

  • I have met the same problem, but I have solved it. How I solved it? This is my answer.

    1. please install the graphviz of 2.3.8.msi。 graphvizzgraphviz

    2. install what you have download the msi package and then add your install path to the environmenttal path like this 'C:\Program Files (x86)\Graphviz2.38; C:\Program Files (x86)\Graphviz2.38\bin'

    3. pip install graphviz.

    4. the following is my result. my xgboost result