安装GraphViz

在Python机器学习实践中需要使用GraphViz显示决策树的图下面是安装、配置及验证过程:
1、官网下载稳定版并安装;
2、将“C:\Program Files (x86)\Graphviz2.38\bin”加入系统变量的“Path”这一步是必须的否则程序报错;
3、pip install graphviz;
4、使用下面代码验证:

import graphviz
import sklearn.datasets as datasets
import pandas as pd

iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns = iris.feature_names)
y = iris.target

from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
dtree.fit(df, y)

from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(dtree, out_file = dot_data, filled = True, rounded = True, special_characters = True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
以上代码在spyder、IDLE中运行不会报错,但是不会显示图,在jupyter notebook 中可以显示图,如下:

猜你喜欢

转载自blog.csdn.net/weixin_38275649/article/details/80246214