Adaboost python
时间: 2023-10-15 11:24:45 浏览: 58
Adaboost (short for Adaptive Boosting) is a popular machine learning algorithm used for classification and regression tasks. It combines multiple weak learners (usually decision trees) into a strong learner.
In Python, you can use the scikit-learn library to implement Adaboost. Here's a simple example:
```python
from sklearn.ensemble import AdaBoostClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Create a synthetic dataset for classification
X, y = make_classification(n_samples=100, n_features=10, random_state=42)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create an AdaBoost classifier with decision tree as base estimator
model = AdaBoostClassifier(n_estimators=50)
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
```
In this example, `AdaBoostClassifier` is used from scikit-learn to create an AdaBoost model with 50 decision tree weak learners. The model is trained using the training data (`X_train` and `y_train`), and predictions are made on the test set (`X_test`). The accuracy of the model is then evaluated using `accuracy_score`.
You can customize the Adaboost algorithm by adjusting parameters like the number of estimators, learning rate, and base estimator. Remember to preprocess your data and tune hyperparameters to get the best performance from the algorithm.