-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBagging.py
141 lines (95 loc) · 3.21 KB
/
Bagging.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
import pandas as pd
import math
Train_data = pd.read_csv('heart_train.data',header = None)
Test_data = pd.read_csv('heart_test.data',header = None)
attrs = len(Train_data.columns) #No of Attributes
rows = len(Train_data) #No of Training Data Points
def splitData(data, attribute):
data0 = data.loc[data[attribute] == 0, :]
data1 = data.loc[data[attribute] == 1, :]
return data0, data1
def label(data):
p = len(data.loc[data[0] == 1, :])
n = len(data.loc[data[0] == 0, :])
if p >= n:
return 1
else:
return 0
def Train(data, attrList):
maxProb = 1
bestAttr = -1
for i in attrList:
prob = 0 #prob of the target given an attribute
for j in range(2): #binary values so j is 0, 1
temp = data.loc[data[i] == j, [0, i]]
tempLen = len(temp)
p = len(temp.loc[temp[0] == 1])
e = len(temp.loc[temp[0] == 0])
logp = 0
loge = 0
if(tempLen > 0):
if p > 0:
logp = (math.log(p/tempLen))/(math.log(2))
if e > 0:
loge = (math.log(e/tempLen))/(math.log(2))
prob = prob + -1 * tempLen/rows * ((p/tempLen)*logp+(e/tempLen)*loge)
if(prob <= maxProb):
maxProb = prob
bestAttr = i
#print(maxProb)
#print(bestAttr)
tree = {}
tree[bestAttr] = {}
data0, data1 = splitData(data, bestAttr)
tree[bestAttr][0] = label(data0)
tree[bestAttr][1] = label(data1)
return tree, bestAttr
def output(tree, lst):
while(True):
for key, value in tree.items():
#print('key', key)
#print('value', value)
if value == 1 or value == 0:
return value
else:
k = lst[key]
tree = tree[key][k]
if tree == 1 or tree == 0:
return tree
def acc(model, data):
r = len(data)
accuracy = 0
for i in range(r):
row = data.loc[i, :]
lst = [0] * 2
for mod in model:
lst[output(mod, row)] += 1
if lst[0] > lst[1]:
if row[0] == 0:
accuracy += 1
else:
if row[0] == 1:
accuracy += 1
accuracy = accuracy/r * 100
return accuracy
it = 25 #Running it for for 25 Iteration to select the best Test accuracy out of 25 Iterations
Test_accuracy = 0
while(it != 0):
n = 20 #20 samplings
classifiers = []
attributeList = []
for i in range(1, attrs):
attributeList.append(i)
for i in range(20):
data = Train_data.sample(n = rows, replace = True)
Hspace, attr = Train(data, attributeList)
attributeList.remove(attr)
classifiers.append(Hspace)
accuracy = acc(classifiers, Test_data)
if accuracy > Test_accuracy:
Test_accuracy = accuracy
print('Iteration', it, 'Accuracy', Test_accuracy)
it -= 1
print("Accuracy on test data set is", Test_accuracy)
print('Classifiers are', classifiers)
print('length', len(classifiers))