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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

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.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

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

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.