adaboost算法Python实现
时间: 2023-09-29 18:03:29 浏览: 102
ada_boost_alpha.zip_Ada Boost 算法_Boost_python写Ada_boost
好的,下面是adaboost算法Python实现的示例代码:
```python
import numpy as np
class AdaBoost:
def __init__(self, n_estimators=50, learning_rate=1.0):
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.estimators = []
self.alphas = []
def fit(self, X, y):
n_samples, n_features = X.shape
w = np.full(n_samples, (1 / n_samples))
for i in range(self.n_estimators):
clf = DecisionTreeClassifier(max_depth=1)
clf.fit(X, y, sample_weight=w)
y_pred = clf.predict(X)
error = np.sum(w * (y_pred != y))
alpha = self.learning_rate * (np.log(1 - error) - np.log(error))
w = w * np.exp(alpha * (y_pred != y))
w = w / np.sum(w)
self.estimators.append(clf)
self.alphas.append(alpha)
def predict(self, X):
n_samples = X.shape[0]
y_pred = np.zeros((n_samples, 1))
for i in range(self.n_estimators):
y_pred += self.alphas[i] * self.estimators[i].predict(X).reshape(-1, 1)
y_pred = np.sign(y_pred)
return y_pred
```
在这个实现中,我们使用了决策树作为弱分类器,每次训练一个弱分类器,并根据其错误率和预测结果更新样本权重,最后将所有弱分类器的预测结果加权求和,得到最终的分类结果。
阅读全文