用 Graphviz 显示这个决策树

Display this decision tree with Graphviz

我正在学习使用 python v3.6 使用 scikit-learn 进行机器学习决策树的教程。

这是代码;

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mglearn
import graphviz

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)

tree = DecisionTreeClassifier(max_depth=4, random_state=0)
tree.fit(X_train, y_train)

from sklearn.tree import export_graphviz
export_graphviz(tree, out_file="tree.dot", class_names=["malignant", "benign"],feature_names=cancer.feature_names, impurity=False, filled=True)

import graphviz
with open("tree.dot") as f:
    dot_graph = f.read()
graphviz.Source(dot_graph)

如何使用 Graphviz 查看里面的内容 dot_graph?据推测,它应该看起来像这样;

graphviz.Source(dot_graph) returns 一个 graphviz.files.Source 对象。

g = graphviz.Source(dot_graph)

使用 g.render() to create an image file. When I ran it on your code without an argument I got a Source.gv.pdf but you can specify a different file name. There is also a shortcut g.view(),它保存文件并在适当的查看器应用程序中打开它。

如果您将代码按原样粘贴到富终端(例如带有内联图形的 Spyder/IPython 或 Jupyter 笔记本),它将自动显示图像而不是对象的 Python 表示。

您可以使用 IPython.display 中的显示。这是一个例子:

from sklearn.tree import DecisionTreeClassifier
from sklearn import tree

model = DecisionTreeClassifier()
model.fit(X, y)

from IPython.display import display
display(graphviz.Source(tree.export_graphviz(model)))

我在 Windows 10 工作。 我通过添加到 'path' 环境变量来解决这个问题。 我添加了错误的路径, 我添加了驱动器:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\lib\site-packages\graphviz 应该用 驱动器:\Users\User.Name\AppData\Local\Continuum\anaconda3\envs\MyVirtualEnv\Library\bin\graphviz 最后我都用了,然后重新启动python/anaconda。 还添加了pydotplus路径,在....MyVirtualEnv\lib\site-packages\pydotplus.

在 jupyter notebook 中,下面绘制了决策树:

from sklearn.tree import DecisionTreeClassifier
from sklearn import tree


model = DecisionTreeClassifier()
model.fit(X, y)
dot_data = tree.export_graphviz(model, 
                  feature_names=feature_names,  
                  class_names=class_names,  
                  filled=True, rounded=True,  
                  special_characters=True,
                   out_file=None,
                           )
graph = graphviz.Source(dot_data)
graph

如果你想保存为png:

graph.format = "png"
graph.render("file_name")

Jupyter 将按原样显示图表,但如果您想放大更多,可以尝试保存文件并进一步检查:

# Draw graph
graph = pydotplus.graph_from_dot_data(dot_data)  

# Show graph
Image(graph.create_png())