adaboost python代码
时间: 2024-12-28 07:14:07 浏览: 7
### 关于AdaBoost算法的Python实现
AdaBoost是一种增强学习方法,通过组合多个弱分类器来创建强分类器。以下是基于决策树桩(Decision Stump)作为基础估计器的一个简单AdaBoost实现例子。
```python
import numpy as np
from sklearn.tree import DecisionTreeClassifier
class AdaBoost:
def __init__(self, n_estimators=50):
self.n_estimators = n_estimators
self.models = []
self.alphas = []
def fit(self, X, y):
n_samples, _ = X.shape
w = np.full(n_samples, (1 / n_samples))
for m in range(self.n_estimators):
model = DecisionTreeClassifier(max_depth=1)
model.fit(X, y, sample_weight=w)
pred = model.predict(X)
error_m = np.sum(w * (pred != y)) / np.sum(w)
alpha_m = 0.5 * np.log((1 - error_m) / (error_m + 1e-10))
w *= np.exp(-alpha_m * y * pred)
w /= np.sum(w)
self.models.append(model)
self.alphas.append(alpha_m)
def predict(self, X):
ensemble_preds = [alpha * model.predict(X) for model, alpha in zip(self.models, self.alphas)]
return np.sign(sum(ensemble_preds)).astype(int)
# Example usage:
if __name__ == "__main__":
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(
n_features=2,
n_redundant=0,
n_informative=2,
random_state=1,
n_clusters_per_class=1
)
y[y == 0] = -1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
clf = AdaBoost()
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
```
此代码定义了一个`AdaBoost`类,该类实现了基本的Adaboost训练逻辑以及预测函数[^1]。对于每一个基分类器,在每一轮迭代过程中计算其权重更新因子α,并据此调整样本分布权值w以便下一次迭代能够更加关注被错误分类的数据点。最终模型预测结果由所有加权后的弱分类器共同决定。
阅读全文