How to implement voting ensemble in Python?

This recipe helps you implement voting ensemble in Python

Recipe Objective

How do you select which model to use for a dataset. We can do this by voting ensemble which trains on an ensemble of numerous models and predicts an output (class) based on their highest probability of chosen class as the output

So this is the recipe on how we can implement voting ensemble in Python.

A Gentle Introduction to Ensemble Learning in Machine Learning

Step 1 - Import the library

from sklearn import model_selection from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.svm import SVC from sklearn.ensemble import VotingClassifier from sklearn import datasets from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt plt.style.use("ggplot")

We have imported various models like LogisticRegression, DecisionTreeClassifier, SVC and VotingClassifier.

Step 2 - Setting up the Data

We have imported Wine dataset and stored the data in X and the target in y. We have used test_train_split to split the data. We have also used model_selection.KFold to split the data. seed = 42 dataset = datasets.load_wine() X = dataset.data; y = dataset.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30) kfold = model_selection.KFold(n_splits=10, random_state=seed)

Step 3 - Selecting model by Voting Classifier

We have made an array named estimators with all the models from e=which we want to select. Now we have used VotingClassifier with parameter as extimator which contain all the models. Finally we have calculated cross validation score of the model. estimators = [] model1 = LogisticRegression(); estimators.append(("logistic", model1)) model2 = DecisionTreeClassifier(); estimators.append(("cart", model2)) model3 = SVC(); estimators.append(("svm", model3)) ensemble = VotingClassifier(estimators) results = model_selection.cross_val_score(ensemble, X_train, y_train, cv=kfold) print(results.mean()) So the output comes as

0.9102564102564104

Download Materials

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

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.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.