-
Notifications
You must be signed in to change notification settings - Fork 1
/
adaboost.py
57 lines (51 loc) · 1.72 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
#!/usr/bin/env python
#coding=utf-8
import sys, math
import numpy
import operator
from abstract_classifier import Classifier
class AdaBoost(Classifier):
def __init__(self, weak_classifier_type):
Classifier.__init__(self)
self.WeakClassifierType = weak_classifier_type
def train(self, T, k = 1):
X = self.X
Y = numpy.array(self.Y)
N = len(self.Y)
w = (1.0/N)*numpy.ones(N)
self.weak_classifier_ensemble = []
self.alpha = []
for t in range(T):
sys.stdout.write('.')
weak_learner = self.WeakClassifierType()
weak_learner.set_training_sample(X,Y)
weak_learner.weights = w
weak_learner.train()
Y_pred = weak_learner.predict(X)
# (Y=-1, Y_pred=1) False Positive
# (Y=1, Y_pred=-1) Missing should be assigned more weights
#ww = numpy.log(k)*(numpy.exp( (Y-Y_pred)>1 ) - 1)/(numpy.exp(1)-1) + 1
e = sum(0.5*w*abs((Y-Y_pred)))/sum(w)
#e = sum(0.5*w*abs(Y-Y_pred))
ee = (1-e)/(e*1.0)
alpha = 0.5*math.log(ee+0.00001)
w *= numpy.exp(-alpha*Y*Y_pred) #*ww) # increase weights for wrongly classified
w /= sum(w)
self.weak_classifier_ensemble.append(weak_learner)
self.alpha.append(alpha)
print "\n"
self.T = T
def predict(self,X):
X = numpy.array(X)
N, d = X.shape
Y = numpy.zeros(N)
for t in range(self.T):
#sys.stdout.write('.')
weak_learner = self.weak_classifier_ensemble[t]
#print Y.shape, self.alpha[t], weak_learner.predict(X).shape
Y += self.alpha[t]*weak_learner.predict(X)
return Y
def test_on_training_set(self, X, Y, T):
self.set_training_sample(X,Y)
self.train(T)
return self.predict(X)