python实现adaboost
时间: 2023-09-05 21:13:50 浏览: 84
以下是Python实现AdaBoost的示例代码:
```
import numpy as np
class AdaBoost:
def __init__(self, n_estimators=50):
self.n_estimators = n_estimators
self.alpha = []
self.estimators = []
def fit(self, X, y):
n_samples = X.shape[0]
weights = np.ones(n_samples) / n_samples
for i in range(self.n_estimators):
tree = DecisionTreeClassifier(max_depth=1)
tree.fit(X, y, sample_weight=weights)
predictions = tree.predict(X)
error = weights.dot(predictions != y)
alpha = 0.5 * np.log((1 - error) / error)
self.alpha.append(alpha)
self.estimators.append(tree)
weights *= np.exp(-alpha * y * predictions)
weights /= np.sum(weights)
def predict(self, X):
predictions = np.zeros(X.shape[0])
for alpha, tree in zip(self.alpha, self.estimators):
predictions += alpha * tree.predict(X)
return np.sign(predictions)
```
该代码使用scikit-learn中的DecisionTreeClassifier作为基本分类器,使用样本权重来训练每个分类器,并使用指数损失函数来计算每个分类器的权重。最终的预测是每个分类器加权的和,其中权重由每个分类器的权重系数alpha给出。
阅读全文