AdaBoost(Adaptive Boosting)是一种常用的集成学习方法,其计算公式为:
其中,是弱分类器,
是权重,
是迭代次数。
该算法是深度学习中常用的集成学习方法之一。
本题使用了分类错误率作为错误率,并使用错误率来更新分类器权重。其公式为:
其中,是分类错误率,
=sum(w[y != prediction])/len(y)。
本题的一个小难点是,若错误率大于0.5,则需要将错误率取反后再进行对比,否则会导致分类器权重更新没有达到预期效果。
标准代码如下
def adaboost_fit(X, y, n_clf):
n_samples, n_features = np.shape(X)
w = np.full(n_samples, (1 / n_samples))
clfs = []
for _ in range(n_clf):
clf = {}
min_error = float('inf')
for feature_i in range(n_features):
feature_values = np.expand_dims(X[:, feature_i], axis=1)
unique_values = np.unique(feature_values)
for threshold in unique_values:
p = 1
prediction = np.ones(np.shape(y))
prediction[X[:, feature_i] < threshold] = -1
error = sum(w[y != prediction])
if error > 0.5:
error = 1 - error
p = -1
if error < min_error:
clf['polarity'] = p
clf['threshold'] = threshold
clf['feature_index'] = feature_i
min_error = error
clf['alpha'] = 0.5 * math.log((1.0 - min_error) / (min_error + 1e-10))
predictions = np.ones(np.shape(y))
if clf['polarity'] == 1:
predictions[X[:, clf['feature_index']] < clf['threshold']] = -1
else:
predictions[X[:, clf['feature_index']] > clf['threshold']] = -1
w *= np.exp(-clf['alpha'] * y * predictions)
w /= np.sum(w)
clfs.append(clf)
return clfs