Mac 安装 Graphviz-python

1. 在系统中安装graphviz

brew install graphviz

2. 在shell中测试一下

dot -V

显示:dot - graphviz version 2.40.1 (20161225.0304)

3. 在python中安装(切换python3的环境)

pip install graphviz 
conda install graphviz

4. 测试import

Mac 安装 Graphviz-python_第1张图片

5. 例子

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())

需要安装一个包:

pip install pydotplus

如果成功了,应该是输出一张图:

Mac 安装 Graphviz-python_第2张图片

 

你可能感兴趣的:(Mac+python)