-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboosting_Adaboost.py
More file actions
28 lines (21 loc) · 1020 Bytes
/
boosting_Adaboost.py
File metadata and controls
28 lines (21 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Load the Iris dataset as an example
iris = load_iris()
X, y = iris.data, iris.target
# Split the dataset 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 (a simple decision tree)
base_classifier = DecisionTreeClassifier(max_depth=1)
# Create an AdaBoost classifier using the base classifier
adaboost_classifier = AdaBoostClassifier(base_classifier, n_estimators=50, random_state=42)
# Fit the classifier to the training data
adaboost_classifier.fit(X_train, y_train)
# Make predictions on the test data
y_pred = adaboost_classifier.predict(X_test)
# Evaluate the accuracy of the AdaBoost classifier
accuracy = accuracy_score(y_test, y_pred)
print(f"AdaBoost Accuracy: {accuracy}")