使用 XGBoost returns Graphviz 错误绘制树

Plotting tree with XGBoost returns Graphviz error

我尝试使用 XGBoost 绘制决策树 . and https://machinelearningmastery.com/visualize-gradient-boosting-decision-trees-xgboost-python/.
我的代码:

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()

模型是用 save_model 方法保存的,并且以其他方式工作(我已经尝试用它进行预测)。唯一的问题是 plot_tree 函数,returns 出现以下错误:

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

我在 Windows(来自下载页面的 "Stable 2.44 Windows install packages")和 Python(pip install graphviz)中都安装了 Graphviz。 Graphviz 在我的 PATH 系统变量中。我究竟做错了什么?我怎样才能解决这个问题?我什至可以解决这个问题,还是 XGBoost 内部的某些东西随着最近的 Graphviz 更新而改变了(他们最近更新了)?

我也遇到过同样的问题,不过已经解决了。我是怎么解决的? 这就是我的答案。

  1. 请安装2.3的graphviz.8.msi。graphvizzgraphviz

  2. 安装你下载的msi包,然后将你的安装路径添加到环境路径中,像这样'C:\Program Files (x86)\Graphviz2.38; C:\Program Files (x86)\Graphviz2.38\bin'

  3. pip 安装 graphviz。

  4. 以下是我的结果。