【问题标题】:Plotting tree with XGBoost returns Graphviz error使用 XGBoost 绘制树返回 Graphviz 错误
【发布时间】:2020-09-22 16:43:56
【问题描述】:

我尝试使用 XGBoost 绘制决策树 Plot a Single XGBoost Decision Tree。和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 函数,它返回以下错误:

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 安装包”)和 Python (pip install graphviz) 中都安装了 Graphviz。 Graphviz 在我的 PATH 系统变量中。我究竟做错了什么?我怎样才能解决这个问题?我什至可以解决这个问题,还是 XGBoost 内部的某些东西随着最近的 Graphviz 更新而改变(他们最近更新了)?

【问题讨论】:

  • 不能使用2.4.4的版本

标签: python graphviz xgboost


【解决方案1】:

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

  1. 请安装2.3.8.msi的graphviz。graphvizzgraphviz

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

  3. pip install graphviz.

  4. 以下是我的结果。

【讨论】:

  • 谢谢!所以正如我所怀疑的那样,Graphviz 在更新过程中引入了一个错误。
猜你喜欢
  • 1970-01-01
  • 2017-01-01
  • 2017-10-09
  • 2018-02-09
  • 2018-12-21
  • 1970-01-01
  • 2016-06-27
  • 2018-12-29
  • 2021-07-27
相关资源
最近更新 更多