-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adaboost_as_coordinate_descent.py
113 lines (65 loc) · 2.26 KB
/
Adaboost_as_coordinate_descent.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
import numpy as np
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
class Adaboost_as_CD:
def __init__(self):
self.weak_clfs = []
self.alphas = []
self.losses = []
def train(self, X, Y):
N, D = X.shape
for t in xrange(D):
choosed_attribute = X[:, t].reshape((N,1))
clf = DecisionTreeClassifier(criterion="entropy", max_depth = 1)
clf.fit(choosed_attribute, Y)
self.weak_clfs.append(clf)
self.alphas = [0 for i in xrange(D)]
Y = Y.reshape((N, 1))
pre_exponential_loss = 0
cur_exponential_loss = 0
while True:
changed = False
for t_prime in xrange(D):
old_alpha_t_prime = self.alphas[t_prime]
h_t_prime = self.weak_clfs[t_prime].predict(X[:, t_prime].reshape((N, 1))).reshape((N, 1))
exponential_term = 0
for t in xrange(D):
if t != t_prime:
exponential_term += self.alphas[t]*self.weak_clfs[t].predict(X[:, t].reshape((N, 1))).reshape((N, 1))
exponential_term = -Y*exponential_term
accuracy = (h_t_prime==Y)
accuracy = np.sum(accuracy*np.exp(exponential_term))
error = (h_t_prime!=Y)
error = np.sum(error*np.exp(exponential_term))
new_alpha_t_prime = 0.5*np.log(accuracy/error)
self.alphas[t_prime] = new_alpha_t_prime
if new_alpha_t_prime - old_alpha_t_prime > 1e-10:
changed = True
cur_exponential_loss = self.exponential_loss(X, Y)
self.losses.append(cur_exponential_loss)
if not changed:
break
pre_exponential_loss = cur_exponential_loss
def exponential_loss(self, X, Y):
N, D = X.shape
prediction = np.zeros((N, 1))
for i in xrange(D):
prediction += self.weak_clfs[i].predict(X[:, i].reshape((N, 1))).reshape((N, 1))*self.alphas[i]
Y = Y.reshape((N, 1))
exponential_term = Y*prediction
loss = np.sum(np.exp(-exponential_term))
return loss
def predict(self, X):
N, D = X.shape
prediction = np.zeros((N, 1))
for i in xrange(D):
prediction += self.weak_clfs[i].predict(X[:, i].reshape((N,1))).reshape((N, 1))*self.alphas[i]
# print prediction
return np.sign(prediction)
def get_accuracy(self, X, Y):
prediction = self.predict(X)
N, _ = X.shape
Y = Y.reshape((N, 1))
assert prediction.shape == Y.shape
accuracy = np.sum(prediction==Y)/float(N)*100
return accuracy