-
Notifications
You must be signed in to change notification settings - Fork 5
/
ensemble.py
239 lines (191 loc) · 7.19 KB
/
ensemble.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
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
import math
from model import Model
from properties import Properties
class Ensemble(object):
def __init__(self, ensemble_size):
self.models = []
self.size = ensemble_size
"""
Update weights for all models in the ensemble.
"""
def updateWeight(self, data, isSource):
for m in self.models:
m.computeModelWeight(data, isSource, Properties.MAXVAR)
def reEvalModelWeights(self, data, maxvar):
for i in range(0, len(self.models)):
self.models[i].weight = self.models[i].computeModelWeightKLIEP(data, maxvar)
"""
Adding a new model to the Ensemble.
Returns the index of the Ensemble array where the model is added.
"""
def __addModelKLIEP(self, model, data, maxvar):
index = 0
self.reEvalModelWeights(data, maxvar)
if len(self.models) < self.size:
self.models.append(model)
index = len(self.models)-1
else:
#replace least desirable model
index = self.__getLeastDesirableModelKLIEP()
if self.models[index].weight < model.weight:
Properties.logger.info('Least desirable model removed at ' + str(index))
self.models[index] = model
else:
Properties.logger.info('New model was not added as its weight is less than all of the existing models')
return -1
return index
"""
Adding a new model to the Ensemble.
Returns the index of the Ensemble array where the model is added.
"""
def __addModel(self, model):
index = 0
if len(self.models) < self.size:
self.models.append(model)
index = len(self.models)-1
else:
#replace least desirable model
index = self.__getLeastDesirableModel()
Properties.logger.info('Least desirable model removed at ' + str(index))
self.models[index] = model
return index
"""
Compute the least desirable model to be replaced when the ensemble size has reached its limit.
Least desirable is one having least target weight
Returns the array index of the least desired model.
"""
def __getLeastDesirableModelKLIEP(self):
weights = {}
for i in xrange(len(self.models)):
weights[i] = self.models[i].weight
keys = sorted(weights, key=weights.get)
return keys[0]
"""
Compute the least desirable model to be replaced when the ensemble size has reached its limit.
Least desirable is one having least target weight, but not the largest source weight.
Returns the array index of the least desired model.
"""
def __getLeastDesirableModel(self):
sweights = {}
tweights = {}
for i in xrange(len(self.models)):
sweights[i] = self.models[i].sweight
tweights[i] = self.models[i].tweight
skeys = sorted(sweights, reverse=True, key=sweights.get)
tkeys = sorted(tweights, key=tweights.get)
# skeys = sweights.keys()
# tkeys = tweights.keys()
for i in xrange(len(skeys)):
if tkeys[i] == skeys[i]:
continue
else:
return tkeys[i]
return tkeys[0]
"""
Initiate the creation of appropriate model in the ensemble for given target data.
Also compute weights for the new model based on the current data.
"""
def generateNewModelKLIEP(self, srcData, srcLabels, trgData, weightSrcData, svmC, svmGamma, svmKernel):
model = Model()
if len(srcData) == 0 or len(trgData) == 0:
raise Exception('Source or Target stream should have some elements')
#Create new model
Properties.logger.info('Target model creation')
model.trainUsingKLIEPWeights(srcData, srcLabels, weightSrcData, Properties.MAXVAR, svmC, svmGamma, svmKernel)
#compute source and target weight
Properties.logger.info('Computing model weights')
model.weight = model.computeModelWeightKLIEP(trgData, Properties.MAXVAR)
#update ensemble
index = self.__addModelKLIEP(model, trgData, Properties.MAXVAR)
if index != -1:
Properties.logger.info('Ensemble updated at ' + str(index))
"""
Initiate the creation of appropriate model in the ensemble for given source or target data.
Also compute weights for the new model based on the current data.
"""
def generateNewModel(self, sourceData, targetData, isSource, useSvmCVParams, svmDefC, svmDefGamma):
model = Model()
if len(sourceData) == 0 or len(targetData) == 0:
raise Exception('Source or Target stream should have some elements')
#Create new model
if isSource:
Properties.logger.info('Source model creation')
model.train(sourceData, None, Properties.MAXVAR, useSvmCVParams, svmDefC, svmDefGamma)
else:
Properties.logger.info('Target model creation')
model.train(sourceData, targetData, Properties.MAXVAR, useSvmCVParams, svmDefC, svmDefGamma)
#compute source and target weight
Properties.logger.info('Computing model weights')
model.computeModelWeight(sourceData, True, Properties.MAXVAR)
model.computeModelWeight(targetData, False, Properties.MAXVAR)
#update ensemble
index = self.__addModel(model)
Properties.logger.info('Ensemble updated at ' + str(index))
"""
Get prediction for a given data instance from each model.
For source data: Ensemble prediction is 1 if maximum weighted vote class label matches true class label, else 0.
For target data: Ensemble prediction class with max weighted vote class label, and average (for all class) confidence measure.
"""
def evaluateEnsembleKLIEP(self, dataInstance):
confSum = {}
weightSum = {}
for m in self.models:
# test data instance in each model
predictedClass, confidence = m.test(dataInstance)
# gather result
if predictedClass[0] in confSum:
confSum[predictedClass[0]] += confidence[0]
weightSum[predictedClass[0]] += m.weight
else:
confSum[predictedClass[0]] = confidence[0]
weightSum[predictedClass[0]] = m.weight
# get maximum voted class label
classMax = 0.0
sorted(confSum, key=confSum.get, reverse=True)
classMax = confSum.keys()[0]
return [classMax, confSum[classMax]/len(self.models)]
"""
Get prediction for a given data instance from each model.
For source data: Ensemble prediction is 1 if maximum weighted vote class label matches true class label, else 0.
For target data: Ensemble prediction class with max weighted vote class label, and average (for all class) confidence measure.
"""
def evaluateEnsemble(self, dataInstance, isSource):
classSum = {}
for m in self.models:
#test data instance in each model
result = m.test([dataInstance], Properties.MAXVAR)
#gather result
if isSource:
if int(result[0][0]) in classSum:
classSum[int(result[0][0])] += m.sweight
else:
classSum[int(result[0][0])] = m.sweight
else:
if int(result[0][0]) in classSum:
classSum[int(result[0][0])] += result[0][1]
else:
classSum[int(result[0][0])] = result[0][1]
#get maximum voted sum class label
classMax = 0.0
sumMax = max(classSum.values())
for i in classSum:
if classSum[i] == sumMax:
classMax = i
if isSource:
#for source data, check true vs predicted class label
if classMax == dataInstance[-1]:
return [1, -1]
else:
return [0, -1]
else:
# for target data
return [classMax, sumMax/len(self.models)]
"""
Get summary of models in ensemble.
"""
def getEnsembleSummary(self):
summry = '************************* E N S E M B L E S U M M A R Y ************************\n'
summry += 'Ensemble has currently ' + str(len(self.models)) + ' models.\n'
for i in xrange(len(self.models)):
summry += 'Model' + str(i+1) + ': weights<' + str(self.models[i].weight) + '>\n'
return summry