1. 程式人生 > >Python 決策樹 生成PDF

Python 決策樹 生成PDF

包準備pydot、graphviz安裝conda install graphviz(完整安裝)pip install pydot       降級安裝示例 pip install robotframework==2.8.7 #生成決策樹

import pandas as pd

import os
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
results=clf.predict_proba(iris.data)#生成PDFfrom sklearn.externals.six import StringIO
with open("iris.dot", 'w') as f:
     f = tree.export_graphviz(clf, out_file=f)
     os.unlink('iris.dot')
from sklearn.externals.six import StringIO 
import pydotplus
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
from IPython.display import Image 
dot_data = StringIO() 
tree.export_graphviz(clf, out_file=dot_data, 
                         feature_names=iris.feature_names, 
                         class_names=iris.target_names, 
                         filled=True, rounded=True, 
                         special_characters=True) 
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
Image(graph.create_png())