How to use Adaboost Classifier and Regressor in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to use Adaboost Classifier and Regressor in Python?

How to use Adaboost Classifier and Regressor in Python?

This recipe helps you use Adaboost Classifier and Regressor in Python

Recipe Objective

Have you ever tried to use Adaboost models ie. regressor or classifier. In this we will using both for different dataset.

So this recipe is a short example of how we can use Adaboost Classifier and Regressor in Python.

Step 1 - Import the library

from sklearn import datasets from sklearn import metrics from sklearn.ensemble import AdaBoostClassifier from sklearn.ensemble import AdaBoostRegressor from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import seaborn as sns

Here we have imported various modules like datasets, AdaBoostClassifier, AdaBoostRegressor, test_train_split, etc from differnt libraries. We will understand the use of these later while using it in the in the code snipet.
For now just have a look on these imports.

Step 2 - Setup the Data for classifier

Here we have used datasets to load the inbuilt iris dataset and we have created objects X and y to store the data and the target value respectively. dataset = datasets.load_iris() X = dataset.data; y = dataset.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

Step 3 - Model and its Score

Here, we are using AdaBoostClassifier as a Machine Learning model to fit the data. model_ABC = AdaBoostClassifier() model_ABC.fit(X_train, y_train) print(model_ABC) Now we have predicted the output by passing X_test and also stored real target in expected_y. expected_y = y_test predicted_y = model_ABC.predict(X_test) Here we have printed classification report and confusion matrix for the classifier. print(metrics.classification_report(expected_y, predicted_y)) print(metrics.confusion_matrix(expected_y, predicted_y))

Step 4 - Setup the Data for regressor

Here we have used datasets to load the inbuilt boston dataset and we have created objects X and y to store the data and the target value respectively. dataset = datasets.load_boston() X = dataset.data; y = dataset.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

Step 5 - Model and its Score

Here, we are using AdaBoostRegressor as a Machine Learning model to fit the data. model_ABR = AdaBoostRegressor() model_ABR.fit(X_train, y_train) print(model_ABR) Now we have predicted the output by passing X_test and also stored real target in expected_y. expected_y = y_test predicted_y = model_ABR.predict(X_test) Here we have printed r2 score and mean squared log error for the Regressor. print(metrics.r2_score(expected_y, predicted_y)) print(metrics.mean_squared_log_error(expected_y, predicted_y)) plt.figure(figsize=(10,10)) sns.regplot(expected_y, predicted_y, fit_reg=True, scatter_kws={"s": 100})

As an output we get:

AdaBoostClassifier(algorithm="SAMME.R", base_estimator=None,
          learning_rate=1.0, n_estimators=50, random_state=None)

              precision    recall  f1-score   support

           0       1.00      1.00      1.00        11
           1       0.84      0.94      0.89        17
           2       0.93      0.82      0.87        17

   micro avg       0.91      0.91      0.91        45
   macro avg       0.93      0.92      0.92        45
weighted avg       0.92      0.91      0.91        45


[[11  0  0]
 [ 0 16  1]
 [ 0  3 14]]

AdaBoostRegressor(base_estimator=None, learning_rate=1.0, loss="linear",
         n_estimators=50, random_state=None)

0.753105739118306

0.03629416190915715

Relevant Projects

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.