-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdt.py
More file actions
626 lines (358 loc) · 11.7 KB
/
dt.py
File metadata and controls
626 lines (358 loc) · 11.7 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import time
# In[5]:
import random
from pprint import pprint
# In[6]:
# Load and prepare data
# The code doesn't take in 0.1 to represent the 10% of the data, it takes in numbers
# In[7]:
df=pd.read_csv("Final.csv")
df= df.rename(columns={"reslt":"label"})
df
# In[8]:
df.info()
# In[9]:
# Train-Test-Split to split data into test and train data
# In[10]:
def train_test_data_split(df, test_size=24):
rows= df.index.tolist() #index each data row in data set
population=rows #population is number of indices
split_size=test_size #the number of samples you want as your test size, as the split number
indices_for_test=random.sample(population,split_size)#indices sampled for the test
test_data=df.loc[indices_for_test]#test dataset
train_data=df.drop(indices_for_test)#dataset for training
return train_data,test_data
# In[11]:
len(df)
# In[12]:
train_df, test_df = train_test_data_split(df, test_size=24)
# In[13]:
len(test_df)
# In[14]:
test_df.head()
# In[15]:
test_df.head()
# In[16]:
test_df.head()
# In[17]:
len(train_df.values)
# In[18]:
train_df.values[:,-1]
# In[20]:
def check_data_purity(data):
if len(np.unique(data[:, -1]))!=1:#if label column has more than 1 value
return False
else:
return True
# In[21]:
data=train_df.values
check_data_purity(data)
# In[23]:
def classify_data(data):
labels=data[:, -1]#retreive label column data
unique,count=np.unique(labels, return_counts=True)#set of unique label values and the number of it
#maximum label value
classification=unique[count.argmax()]
return classification
# In[24]:
# Potential Splits
# In[25]:
def getting_potential_splits(data):
potential_splits={}#empty dict
data.shape
_, n_columns = data.shape
for i in range(n_columns-1):#i is the column index
potential_splits[i]=[]#empty list, list in dictionary, as listed below
values=data[:, i]
unique_values=np.unique(values)
n=len(unique_values)
for j in range(n-1):
if j!=n-1:#avoids the first, so can access previous
current_unique=unique_values[j]
next_unique=unique_values[j+1]
potential_split= (current_unique+next_unique)/2 #average between 2 unique values
potential_splits[i].append(potential_split)
return potential_splits
# In[68]:
# data=train_df.values
potential_splits=getting_potential_splits(train_df.values)
# In[27]:
sns.lmplot(data=train_df,x="age",y="res",hue="label", fit_reg=False, height=6, aspect=1.5)
# plt.vlines(x=potential_splits[1], ymin=30, ymax=65)
# plt.hlines(y=potential_splits[7], xmin=17, xmax=38)
plt.hlines(y=potential_splits[7], xmin=17, xmax=38) # draw lines between data points
plt.vlines(x=potential_splits[1], ymin=1, ymax=2)
# In[28]:
# Split Data
# In[29]:
def split_data(data, split_column, split_value):
split_column_values=data[:,split_column]# values of teh column whose values you want to split by
data_below=data[split_column_values<=split_value]#split values of that column by the value you want to split by
data_above=data[split_column_values>split_value]
return data_below, data_above
# In[30]:
data=train_df.values
# In[31]:
data
# In[32]:
split_column=2
split_value=37.5
# In[66]:
data_below, data_above=split_data(data, split_column, split_value)
# In[67]:
plotting_df=pd.DataFrame(data, columns=df.columns)#datra and columns
sns.lmplot(data=plotting_df, x="BP",y="weight",hue="label", fit_reg=False, height=6, aspect=1.5)
plt.hlines(y=split_value, xmin=1, xmax=3)
# In[35]:
# Highest Information Gain
# In[36]:
def calculate_entropy(data):
labels=data[:,-1]
_, counts=np.unique(labels, return_counts=True)#counts is a list
probabilities=counts/counts.sum()# probability based on number of classes for the list
entropy=sum(probabilities*(-np.log2(probabilities)))# H =sum(pilogpi)
return entropy
# In[37]:
calculate_entropy(data)
# In[38]:
def calculate_overall_entropy(data_below, data_above):
n=len(data_below)+len(data_above)
probability_data_below=len(data_below)/n
probability_data_above=len(data_above)/n
overall_entropy=(probability_data_below*calculate_entropy(data_below)+probability_data_above*calculate_entropy(data_above))
#databelow(sum(plogp))+dataabove(sum(plogp))
return overall_entropy
# In[39]:
calculate_overall_entropy(data_below,data_above)
# In[40]:
def get_best_split(data, potential_splits):
overall_entropy=9999 #arbitrary large number
for column_index in potential_splits: #the previously dict
for value in potential_splits[column_index]:#list in dict
split_column=column_index
split_value=value
data_below, data_above=split_data(data, split_column,split_value)
current_overall_entropy=calculate_overall_entropy(data_below, data_above)
if current_overall_entropy<= overall_entropy:
overall_entropy=current_overall_entropy #want least entropy for highest information gain
best_split_column=column_index #want to know which column to split at
best_split_value=value #which value you should split at for that column
return best_split_column, best_split_value
# In[41]:
potential_splits=getting_potential_splits(data)
# In[42]:
data=train_df.values
get_best_split(data, potential_splits)
# In[43]:
# Decision Tree Algorithm
# In[44]:
# sub_tree={question:[yes_answer,no_answer]}
# In[45]:
{"weight<=37.5":[0,1]}
# In[46]:
# Algorithm
# In[47]:
def decision_tree_algorithm(df, counter=0, min_samples=1, max_depth=7):
#data preparations
if counter==0:
global COLUMN_HEADERS #global to access
COLUMN_HEADERS=df.columns
data=df.values #numpy array if first iteration
else:
data=df #later iterations
#base case
if (check_data_purity(data)) or (counter==max_depth) or (len(data)<min_samples) :
classification=classify_data(data) #direct classification
return classification
#recursive part
else:
counter+=1
#impure data
#helper functions
potential_splits=getting_potential_splits(data)
split_column, split_value=get_best_split(data, potential_splits)
data_below, data_above = split_data(data, split_column, split_value)
#instantiate subtree
feature_name=COLUMN_HEADERS[split_column]# get the name of the column, not just ind value
question="{} <= {}".format(feature_name, split_value)
sub_tree={question:[]}#answer could be a list
#find answers
yes_answer=decision_tree_algorithm(data_below, counter, min_samples, max_depth)
no_answer=decision_tree_algorithm(data_above, counter, min_samples, max_depth)
if yes_answer==no_answer:#if greater than a max depth, the yes and no answers tend to be the same
sub_tree= yes_answer
else:
sub_tree[question].append(yes_answer)
sub_tree[question].append(no_answer)
return sub_tree
# In[48]:
#pruning
tree=decision_tree_algorithm(train_df, max_depth=5)#can give shorter trees and more accuracy
pprint(tree)
# In[49]:
df.columns
# In[50]:
# Classification
# In[51]:
example=test_df.iloc[2]#2nd index row
example
# In[52]:
def classify_example(example, tree):
question=list(tree.keys())[0] #the first question
feature_name, comparison_operator, value=question.split()#all 3 stored separately
#ask question
if example[feature_name]<=float(value):#"weight<=38.5 similar"
answer=tree[question][0]#yes
else:
answer=tree[question][1]#no
#base case
if not isinstance(answer, dict): #if answer is single
return answer
#recursive call of function
else:
residual_tree=answer #if answer is a list, another condition
return classify_example(example, residual_tree)
# In[53]:
classify_example(example, tree)
# In[54]:
example["weight"]<=37.5
# In[55]:
# Accuracy
# In[56]:
def calculate_accuracy(df, tree):
df["classification"]=df.apply(classify_example, axis=1, args=(tree,))#create new column in df to compare easily
df["classification_correct"]=df.classification==df.label#true if classified correctly
accuracy=df.classification_correct.mean()*100 #accuracy percentage
return accuracy
# In[57]:
calculate_accuracy(test_df, tree)
# In[58]:
#test_df.loc[6]
# In[59]:
random.seed(2)
train_df, test_df =train_test_data_split(df, test_size=24)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
pprint(tree)
print(accuracy)
# In[61]:
test_df
# In[62]:
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=24)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[63]:
random.seed(3)
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=24)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[217]:
random.seed(4)
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=24)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[165]:
random.seed(5)
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=11)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[166]:
random.seed(6)
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=10)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[167]:
random.seed(6)
summation=0
for i in range(1,10000,1):
train_df, test_df =train_test_data_split(df, test_size=10)
tree=decision_tree_algorithm(train_df,max_depth=3)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10000
#pprint(tree)
#print(accuracy)
total
# In[248]:
0.1*106
# In[329]:
random.seed(7)
summation=0
s_time=0
for i in range(1,101,1):
train_df, test_df =train_test_data_split(df, test_size=10)
start=time.time()
tree=decision_tree_algorithm(train_df,max_depth=3)
end=time.time()
s_time=s_time+(end-start)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10
#pprint(tree)
#print(accuracy)
total
# In[330]:
s_time
# In[92]:
random.seed(8)
summation=0
s_time=0
for i in range(1,10001,1):
train_df, test_df =train_test_data_split(df, test_size=10)
start=time.time()
tree=decision_tree_algorithm(train_df,max_depth=3)
end=time.time()
s_time=s_time+(end-start)
accuracy=calculate_accuracy(test_df,tree)
summation=summation+accuracy
total=summation/10001
#pprint(tree)
#print(accuracy)
total
s_time
# In[94]:
s_time
# In[93]:
total