-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaboost.py
199 lines (122 loc) · 6.84 KB
/
adaboost.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 29 01:40:00 2018
@author: nithish k
Please refer the report in the pdf commited in the git hub
"""
import pandas as pd
import numpy as np
import itertools as itr
import random
import collections as col
import math
import time
class AdaBoost():
def __init__(self,nTrees = 50 ,**params):
self.trainData = []
self.nTrees = nTrees
self.verbose = params.get('verbose',False)
self.isTrained = False
self._decisionsStumps = None ##feature pair as key
self._stumpWeights = None
pass
def verbosePrint(self,*args):
if self.verbose:
print(*args)
def _getDecisionPairs(self,numStumps):
random.seed(42)
combinations = itr.combinations(range(self.trainXMatrix.shape[1]),2)
# randomCombinations = random.choices(list(combinations),k=numStumps)
return list(combinations)
def _makeDecisions(self,featurePair):
##rerutns segregated observations
majorityClass = col.defaultdict(int)
feature1 , feature2 = featurePair ##tuple (x1,x2)
booleanList = self.trainXMatrix[:,feature1] >= self.trainXMatrix[:,feature2]
##mejority class in positive decision
majorityClass['Positive'] = col.Counter(self.trainYLabels[booleanList]).most_common(1)[0][0]
negatedBooleanList = np.logical_not(booleanList)
##mejority class in negative decision
majorityClass['Negative'] = col.Counter(self.trainYLabels[negatedBooleanList]).most_common(1)[0][0]
mappingDict = {True: majorityClass['Positive'],False: majorityClass['Negative']}
predictionList = pd.Series(booleanList).map(mappingDict)
return majorityClass,predictionList
def _calcError(self,obsWeights,predictionList, YActual):
##pass obsWeights as np.array
misClassifiedList = predictionList != YActual
weightedError = sum(np.array(obsWeights)[misClassifiedList])/sum(obsWeights)
# print(weightedError)
return weightedError
def _adjustNormWeights(self,currentObsWeights,stumpWeight,predictionList,YActual):
updatedObsWeights = currentObsWeights.copy()
misClassifiedList = predictionList != YActual
updatedObsWeights[misClassifiedList] = updatedObsWeights[misClassifiedList]*math.exp(stumpWeight)
sumObsWeights = np.sum(updatedObsWeights)
updatedObsWeights = updatedObsWeights/sumObsWeights
return updatedObsWeights
def train(self,TrainXmatrix,TrainY):
self.trainXMatrix = TrainXmatrix
self.trainYLabels = TrainY
uniqueYLables = set(TrainY)
numYLabels = len(uniqueYLables)
AllfeatureCombinations = self._getDecisionPairs(self.nTrees)
decisionsForFeatures = col.defaultdict(dict)
weightsForDecisions = col.defaultdict(int)
numTrainObs = self.trainXMatrix.shape[0]
obsWeights = np.array([1/numTrainObs for i in range(numTrainObs)]) ##initialise
numSatisifyingStumps =0
while numSatisifyingStumps < self.nTrees:
randomCombinations = random.choices(AllfeatureCombinations,k=self.nTrees)
AllfeatureCombinations = list(set(AllfeatureCombinations)-set(randomCombinations))
if len(AllfeatureCombinations)==0:
break
for featurePair in randomCombinations: ##feature pair as tuple
decisionStump, trainPredictionList = self._makeDecisions(featurePair)
stumpError = self._calcError(obsWeights,trainPredictionList,self.trainYLabels)
if stumpError > 1-(1/numYLabels): #not better than random guessing
continue
numSatisifyingStumps+=1
stumpWeight = math.log((1-stumpError)/stumpError) + math.log(numYLabels-1)
obsWeights = self._adjustNormWeights(obsWeights,stumpWeight,trainPredictionList,self.trainYLabels)
decisionsForFeatures[featurePair] = decisionStump
weightsForDecisions[featurePair] = stumpWeight
self.verbosePrint("Gathered useful stumps :" ,numSatisifyingStumps)
self._decisionsStumps = decisionsForFeatures ##feature pair as key
self._stumpWeights = weightsForDecisions ##
self.isTrained = True
def getDataFromFile(self, filename):
DataDf = pd.read_csv(filename,header = None, sep = ' ' )
DataDf.columns = ['photo_id','correct_orientation'] + [i-2 for i in DataDf.columns[2:].tolist()]
XDataMatrix = np.array(DataDf.loc[:,~DataDf.columns.isin(['photo_id','correct_orientation'])])
YLabels = DataDf['correct_orientation']
XDataID = DataDf['photo_id']
return XDataMatrix,YLabels,XDataID
def predict(self, TestXmatrix):
dictOfLabelsCumWeights = col.defaultdict(lambda: col.defaultdict(int))
self.verbosePrint("\nPredicting.....")
for (feature1,feature2),decisionNode, in self._decisionsStumps.items():
booleanList = TestXmatrix[:,feature1] >= TestXmatrix[:,feature2]
mappingDict = {True: decisionNode['Positive'],False: decisionNode['Negative']}
predictionList = pd.Series(booleanList).map(mappingDict)
decisionWeight = self._stumpWeights[(feature1,feature2)]
for i,label in enumerate(predictionList):
dictOfLabelsCumWeights[i][label] += decisionWeight
dictOfLabelsCumWeights = dict(dictOfLabelsCumWeights)
finalWeightedPredictions = \
[max(dictOfLabelsCumWeights[i],key = dictOfLabelsCumWeights[i].get) for i in range(len(predictionList))]
return finalWeightedPredictions
def writeToFile(self,ID,predictionList,filename):
OutputDf = pd.DataFrame({'ID':ID,'Predictions':predictionList})
OutputDf.to_csv(path_or_buf = filename ,sep = ' ',header = False ,index = False)
if __name__ == '__main__':
myBoost = AdaBoost(200,verbose = True)
TrainX,TrainY,TrainXID = myBoost.getDataFromFile('train-data.txt')
#print(myBoost.train('Data'))
start = time.time()
myBoost.train(TrainX,TrainY)
Xtest,yTest,XtestID = myBoost.getDataFromFile('test-data.txt')
finalPredictions = myBoost.predict(Xtest)
myBoost.writeToFile(XtestID,finalPredictions,'output.txt')
print("Time elapsed is ", time.time()-start)
print("Accuracy is: " ,sum(finalPredictions==yTest)/len(yTest))