How to apply adaboost or classification in R

This recipe helps you apply adaboost or classification in R

Recipe Objective

How to apply adaboost or classification in R

Classification and regression are supervised learning models that can be solved using algorithms like linear regression / logistics regression, decision tree, etc. But these are not competitive in terms of producing a good prediction accuracy. Ensemble techniques, on the other hand, create multiple models and combine them into one to produce effective results. Bagging, boosting, random forest, are different types of ensemble techniques. Boosting is a sequential ensemble technique in which the model is improved using the information from previously grown weaker models. This process is continued for multiple iterations until a final model is built which will predict a more accurate outcome. There are 3 types of boosting techniques: 1. Adaboost 2. Gradient Descent. 3. Xgboost Adaboost i.e Adaptive boosting is a boosting technique that improves the weak learner (models) by aggregating the models and creating a new improved model. The adaboost algorithm improves the performance of the weak learners by increasing the weights to create a better final model. The following recipe explains how to apply adaboost for classification in R

Step 1 - Install the necessary libraries

install.packages('adabag') # for fitting the adaboost model install.packages('caret') # for general data preparation and model fitting library(adabag) library(caret)

Step 2 - Read a csv file and explore the data

data <- iris # reads the dataset head(data) # head() returns the top 6 rows of the dataframe summary(data) # returns the statistical summary of the data columns dim(data)

Step 3 - Train and Test data

# createDataPartition() function from the caret package to split the original dataset into a training and testing set and split data into training (80%) and testing set (20%) parts = createDataPartition(data$Species, p = 0.8, list = F) train = data[parts, ] test = data[-parts, ]

Step 4 - Create a adaboost model

# train a model using our training data model_adaboost <- boosting(Species~., data=train, boos=TRUE, mfinal=50) summary(model_adaboost)

Step 5 - Make predictions on the test dataset

#use model to make predictions on test data pred_test = predict(model_adaboost, test) # Returns the prediction values of test data along with the confusion matrix pred_test accuracy_model <- (10+9+8)/30 accuracy_model

The prediction : Setosa : predicted all 10 correctly versicolor : predicted 9 correctly, falsely predicted 2 as virginica virginica : predicted all 8 correctly, falsely predicted 1 as versicolor. The model gives a error of 10% and accuarcy of 90%.

{"mode":"full","isActive":false}

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .