-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
53 lines (39 loc) · 1.59 KB
/
test.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
import numpy as np
from copy import deepcopy
from sklearn.tree import DecisionTreeClassifier
class AdaBoost:
"""
input:n_estimators(int):迭代轮数
learning_rate(float):弱分类器权重缩减系数
"""
def __init__(self, n_estimators=50, learning_rate=1.0):
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.base_estimator = DecisionTreeClassifier()
def fit(self, X, y):
sample_weight = np.empty(X.shape[0], dtype=np.float64)
sample_weight[:] = 1. / X.shape[0]
self.estimators_ = []
self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float64)
self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float64)
for iboost in range(self.n_estimators):
sample_weight, estimator_weight, estimator_error, estimator = self._boost(
X, y, sample_weight
)
if estimator_error > 0.5:
break
self.estimators_.append(estimator)
self.estimator_weights_[iboost] = estimator_weight
self.estimator_errors_[iboost] = estimator_error
if estimator_error <= 0:
break
return self
estimator.fit(X, y, sample_weight=sample_weight)
y_pred = estimator.predict(X)
estimator_error = np.mean(y_pred != y)
# estimator is perfect
if estimator_error <= 0:
return 0, 1, estimator_error, estimator
# estimator is weak
if estimator_error > 0.5:
return 0, 0, estimator_error, estimator