We are going to implement Adaboost algorithm in Python using Scikit Learn library. This is an ensemble learning technique and we will use AdaBoostClassifier to solve IRIS dataset problem.
You can download my Jupyter notebook implementing Adaboost from here.
Step 1: Import the required Python libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
Step 2: Load and examine the dataset
dataset = datasets.load_iris()
dataset.feature_names
dataset.target_names
dataset.data.shape
dataset.target.shape
dataset.data[0:5]
dataset.target[0:5]
Step 3: Mention X and Y axis
X = dataset.data
y = dataset.target
Step 4: Split the dataset into training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
Step 5: Create and fit the model
model = AdaBoostClassifier(n_estimators=50, learning_rate=1)
model.fit(X_train, y_train)
Step 6: Predict from the model
y_pred = model.predict(X_test)
Step 7: Check the accuracy
confusionMatrix = confusion_matrix(y_test, y_pred)
accuracyScore = accuracy_score(y_test, y_pred)
classificationReport = classification_report(y_test, y_pred)
print(confusionMatrix)
print(accuracyScore * 100)
print(classificationReport)
To learn more about Adaboost, you can refer my below posts:
Difference between Random Forest and AdaBoost in Machine Learning
Difference between AdaBoost and Gradient Boosting Machine (GBM)
You can download my Jupyter notebook implementing Adaboost from here.
Step 1: Import the required Python libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
Step 2: Load and examine the dataset
dataset = datasets.load_iris()
dataset.feature_names
dataset.target_names
dataset.data.shape
dataset.target.shape
dataset.data[0:5]
dataset.target[0:5]
Step 3: Mention X and Y axis
X = dataset.data
y = dataset.target
Step 4: Split the dataset into training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
Step 5: Create and fit the model
model = AdaBoostClassifier(n_estimators=50, learning_rate=1)
model.fit(X_train, y_train)
Step 6: Predict from the model
y_pred = model.predict(X_test)
Step 7: Check the accuracy
confusionMatrix = confusion_matrix(y_test, y_pred)
accuracyScore = accuracy_score(y_test, y_pred)
classificationReport = classification_report(y_test, y_pred)
print(confusionMatrix)
print(accuracyScore * 100)
print(classificationReport)
To learn more about Adaboost, you can refer my below posts:
Difference between Random Forest and AdaBoost in Machine Learning
Difference between AdaBoost and Gradient Boosting Machine (GBM)
No comments:
Post a Comment