出现bug
在调用 GraphViz 的 "dot" 命令时发生了问题。AssertionError: "dot" with args...通常是因为 GraphViz 没有正确安装或系统路径没有设置正确。
请按照以下步骤进行操作:
确保您已经安装了 GraphViz。如果尚未安装,请访问 GraphViz 官方网站(https://graphviz.org/download/ )下载并安装。
安装完成后,您需要将 GraphViz 的可执行文件添加到系统的 PATH 变量中。在 Windows 上,通常可以在 C:\Program Files (x86)\GraphvizX.XX\bin 或 C:\Program Files\GraphvizX.XX\bin(其中 X.XX 是 GraphViz 的版本号)找到这些文件。
将 GraphViz 的 bin 目录添加到系统的 PATH 变量中。您可以按照以下步骤操作:
打开“控制面板”。
点击“系统”。
点击“高级系统设置”。
在“高级”选项卡下,点击“环境变量”。
在“系统变量”部分,找到名为“Path”的变量并双击它。
在“编辑环境变量”窗口中,点击“新建”,然后将 GraphViz 的 bin 目录路径粘贴到其中。
点击“确定”保存更改。
重启您的 Python 开发环境(例如 Jupyter Notebook 或 IDE),然后重新运行代码。这样应该可以解决问题。
如果问题仍然存在,请确保您已正确安装并导入相关库(例如 sklearn、pydot 和 graphviz)。
StringI0和StringIO 的拼写错误。
from io import StringIO
from sklearn.tree import export_graphviz
from pydot import graph_from_dot_data
from IPython.display import Image
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data, filled=True, rounded=True, special_characters=True,
feature_names=feature_cols, class_names=['0', '1', '2', '3', '4', '5', '6'])
(graph,) = graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())