-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecision tree Live.py
75 lines (39 loc) · 1.09 KB
/
Decision tree Live.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
# In[2]:
iris = datasets.load_iris()
X = iris.data
y = iris.target
# In[3]:
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,y)
# In[9]:
fig = plt.figure(figsize=(25,20))
tree.plot_tree(clf, filled = True, rounded = True, max_depth=3)
plt.show()
# In[10]:
get_ipython().system('pip install graphviz')
# In[12]:
import graphviz
data = tree.export_graphviz(clf,out_file = None,
feature_names=iris.feature_names,
class_names = iris.target_names,
filled=True)
graph = graphviz.Source(data, format='png')
graph
# In[13]:
graph.render("decision_tree_graphviz")
# In[14]:
from dtreeviz.trees import dtreeviz
tree = dtreeviz(clf, X, y,
target_name = "target",
feature_names = iris.feature_names,
class_names = list(iris.target_names))
tree
# In[15]:
tree.save("decision_tree.svg")