-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdt_visualization_graphviz.py
More file actions
28 lines (24 loc) · 1.03 KB
/
dt_visualization_graphviz.py
File metadata and controls
28 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from sklearn import datasets
iris=datasets.load_iris()
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
#----------------------------------------------------------------------
#Code to create an object of decision tree and building a decision tree.
#----------------------------------------------------------------------
df1=pd.DataFrame(iris.data,columns=iris.feature_names)
y=iris.target
dtree=DecisionTreeClassifier()
dtree.fit(df1,y)
#----------------------------------------------------------------------
#Code for visualization of the decision tree in human readable format
#----------------------------------------------------------------------
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())