-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaboost.py
140 lines (111 loc) · 4.46 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
from collections.abc import Sequence, Callable
from concurrent.futures import ThreadPoolExecutor
import numpy as np
class AdaBoost:
'''
AdaBoost classifier using a set of weak classifiers.
References
----------
[1] Yoav Freund and Robert E Schapire. "A decision-theoretic generalization of on-line
learning and an application to boosting". Journal of Computer and System Sciences,
55(1):119-139, 1997. https://www.sciencedirect.com/science/article/pii/S002200009791504X.
'''
def __init__(
self, weak_classifiers: Sequence[Callable[[np.ndarray], np.ndarray]], n_estimators: int = 50, num_workers: int = 1
) -> None:
'''
Initialize the AdaBoost classifier.
Parameters
----------
weak_classifiers : list[Callable[[np.ndarray], np.ndarray]]
List of weak classifiers to choose from. Each classifier should map a 2-d numpy array of
samples to 1-d numpy array of binary classes.
n_estimators : int
Number of weak classifiers to use. Default is 50.
'''
self.weak_classifiers = list(weak_classifiers)
self.n_estimators = n_estimators
self.num_workers = num_workers
self.alphas = []
self.selected_classifiers = []
def fit(self, X: np.ndarray, y: np.ndarray) -> 'AdaBoost':
'''
Fit the AdaBoost classifier to the training data.
Parameters
----------
X : np.ndarray
Training features of shape (n_samples, n_features)
y : np.ndarray
Target labels of shape (n_samples,)
Returns
-------
self
'''
n_samples = X.shape[0]
w = np.ones(n_samples) / n_samples # Initialize weights uniformly
for _ in range(self.n_estimators):
# Calculate error for each weak classifier
weak_clf_preds = [clf(X) for clf in self.weak_classifiers]
errors = np.asarray([np.sum(w[y != preds]) for preds in weak_clf_preds])
# Select the best weak classifier
best_clf_idx = np.argmin(errors)
best_clf = self.weak_classifiers[best_clf_idx]
# Calculate classifier weight
error = errors[best_clf_idx]
if error >= 1.0:
break
alpha = 0.5 * np.log((1 - error) / (error + 1e-10))
# Update sample weights
predictions = weak_clf_preds[best_clf_idx]
w *= np.exp(-alpha * y * predictions)
w /= w.sum() # Normalize weights
# Store the selected classifier and its weight
self.alphas.append(alpha)
self.selected_classifiers.append(best_clf)
return self
def predict(self, X: np.ndarray) -> np.ndarray:
'''
Make predictions using the trained AdaBoost classifier.
Parameters
----------
X : np.ndarray
Input features of shape (n_samples, n_features)
Returns
-------
np.ndarray
Predictions of shape (n_samples,)
'''
if self.num_workers == 1:
clf_preds = np.array([clf(X) for clf in self.selected_classifiers])
else:
with ThreadPoolExecutor(max_workers=self.num_workers) as executor:
clf_preds = np.asarray(list(executor.map(lambda clf: clf(X), self.selected_classifiers)))
preds = np.where(np.dot(self.alphas, clf_preds) > 0, 1, -1)
return preds
def score(self, X: np.ndarray, y: np.ndarray) -> float:
'''
Compute the accuracy of the AdaBoost classifier on inputs X against targets y.
Parameters
----------
X : np.ndarray
Feature matrix of shape (n_samples, n_features)
y : np.ndarray
Target vector of shape (n_samples,)
Returns
-------
float
The accuracy of the AdaBoost Classifier.
'''
preds = self.predict(X)
acc = np.mean(preds == y)
return acc
def __call__(self, *args, **kwargs):
return self.predict(*args, **kwargs)
def __str__(self):
return f'{self.__class__.__name__}(n_estimators={self.n_estimators})'
def __repr__(self):
repr_str = self.__str__()
clf_limit = 5
clf_str = f'[{", ".join(map(str, self.weak_classifiers[:clf_limit]))}{", ..." * (len(self.weak_classifiers) > clf_limit)}]'
repr_str = repr_str[:-2] + f', weak_classifiers={clf_str})'
return repr_str