-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecisionTree.py
131 lines (60 loc) · 1.85 KB
/
DecisionTree.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import numpy as np
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import graphviz
import sklearn.datasets as dtst
# In[2]:
#np.random.seed(4)
df = pd.read_csv('data.csv')
# In[3]:
Y = df['class'].values
X = df
del X['class']
del X['Unnamed: 0']
cols = X.columns
numeric = ['age', 'bp', 'sg', 'al', 'su', 'bgr', 'bu', 'sc', 'sod', 'pot', 'hemo', 'pcv', 'wbcc', 'rbcc']
for i in numeric:
d = pd.DataFrame(X[i], columns=[i])
X = d.values
# In[4]:
#emp = None
#for j in cols:
# if emp is None:
# emp = pd.DataFrame(X[j], columns=[j])
# else:
# emp = emp.join(X[j])
# if not j in numeric:
# emp = pd.get_dummies(emp, columns=[j])
#X = emp.values
# In[5]:
encoder = LabelEncoder()
encoder.fit(Y)
Y = encoder.transform(Y)
# In[6]:
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)
# In[7]:
model = DecisionTreeClassifier()
model.fit(X_train,Y_train)
score = model.score(X_test, Y_test)
ypred = model.predict(X_test)
ytest = Y_test
print('Accuracy using Decision Tree Classifier: ', score*100, '%')
print(classification_report(ypred,ytest))
print(confusion_matrix(ypred,ytest))
from sklearn.datasets import load_iris
iris = load_iris()
model = model.fit(iris.data,iris.target)
from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
#import pydotplus
#dot_data = stringIO()
dot_data = export_graphviz(model, out_file=None)
graph = graphviz.Source(dot_data)
graph.render('Iris')
graph = graphviz.Source(dot_data)
graph