在Colaboratory中可视化决策树

4
什么是使用Google Colab可视化决策树的最佳方法?'dtreeviz'提供的可视化效果非常好(例如:GitHub),但是当运行类似下面的内容时,……
!pip install dtreeviz

并且

from sklearn.datasets import *
from sklearn import tree
from dtreeviz.trees import *

紧随其后
classifier = tree.DecisionTreeClassifier(max_depth=4)
cancer = load_breast_cancer()
classifier.fit(cancer.data, cancer.target)
viz = dtreeviz(classifier,
              cancer.data,
              cancer.target,
              target_name='cancer',
              feature_names=cancer.feature_names, 
              class_names=["malignant", "benign"],
              fancy=False )  

viz.view()

我理解

ExecutableNotFound: failed to execute ['dot', '-Tsvg', '-o', '/tmp/DTreeViz_62.svg', '/tmp/DTreeViz_62'], make sure the Graphviz executables are on your systems' PATH

这可能与Colab通过我的g-drive运行有关?

非常感谢您的帮助!


你尝试过 !apt-get install graphviz 吗? - Maximilian Peters
@MaximilianPeters,谢谢 - 我现在尝试了。仍然没有返回可视化 - 但是现在也没有错误消息。 - RandomForestRanger
哈哈,我们快到了!你可以尝试使用 from IPython.core.display import display, HTML; display(HTML(viz.svg()) 吗? - Maximilian Peters
哇,坚持不懈的顶级人物。而且...你解决了它!我在你的代码末尾添加了一个")"。 - RandomForestRanger
1个回答

3

简短回答

  • Make sure graphviz is installed via !apt-get install graphviz
  • You can get the created SVG via viz.svg()
  • Wrap the output in IPython's HTML and then call display to show it in your notebook

    from IPython.core.display import display, HTML
    display(HTML(viz.svg()))
    

更详细的回答

  • dtreeviz view() 创建一个 SVG 文件在你的临时目录中
  • 该文件将被传递给 graphviz 库,它会根据你的操作系统打开它
  • Google Colab 被识别为 linux 并尝试通过默认查看应用程序打开 SVG 文件
  • 如果您不在本地运行笔记本电脑(可能是 Google 服务器现在有一些打开的 SVG 图像或某些错误消息),则最后一步将无处可去。
  • 简短回答中的代码只获取 SVG 代码而不保存它,然后在笔记本中显示它

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接