bagging python
时间: 2023-07-15 14:59:41 浏览: 126
基于sklearn实现Bagging算法(python)
Bagging, short for bootstrap aggregating, is a popular ensemble learning technique in machine learning. It is used to improve the performance and robustness of predictive models. Bagging involves training multiple base models on different subsets of the training data and then combining their predictions.
In Python, you can implement bagging using various machine learning libraries such as scikit-learn. The scikit-learn library provides a BaggingClassifier class that can be used for bagging classification tasks. Similarly, the BaggingRegressor class can be used for regression tasks.
Here's a simple example of bagging using scikit-learn:
```python
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the Iris dataset
data = load_iris()
X = data.data
y = data.target
# 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 a base classifier
base_classifier = DecisionTreeClassifier()
# Create a bagging classifier
bagging_classifier = BaggingClassifier(base_estimator=base_classifier, n_estimators=10)
# Train the bagging classifier
bagging_classifier.fit(X_train, y_train)
# Make predictions on the test set
predictions = bagging_classifier.predict(X_test)
# Evaluate the accuracy of the predictions
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
```
In this example, we use the Iris dataset and a decision tree as the base classifier. We create a bagging classifier with 10 estimators (base models) and train it on the training data. We then make predictions on the test set and evaluate the accuracy of the predictions.
Note that this is just a basic example, and there are many variations and extensions of bagging that you can explore depending on your specific use case and the machine learning library you are using.
阅读全文