How to implement voting ensemble in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to implement voting ensemble in Python?

How to implement voting ensemble in Python?

This recipe helps you implement voting ensemble in Python

0

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.

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

Relevant Projects

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.