Voting is an ensemble machine learning algorithm.
For regression, a voting ensemble involves making a prediction that is the average of multiple other regression models.
In classification, a hard voting ensemble involves summing the votes for crisp class labels from other models and predicting the class with the most votes.
A soft voting ensemble involves summing the predicted probabilities for class labels and predicting the class label with the largest sum probability.
In this tutorial, you will discover how to create voting ensembles for machine learning algorithms in Python.
After completing this tutorial, you will know:Let’s get started.
How to Develop Voting Ensembles With PythonPhoto by Bureau of Land Management, some rights reserved.
This tutorial is divided into four parts; they are:A voting ensemble (or a “majority voting ensemble“) is an ensemble machine learning model that combines the predictions from multiple other models.
It is a technique that may be used to improve model performance, ideally achieving better performance than any single model used in the ensemble.
A voting ensemble works by combining the predictions from multiple models.
It can be used for classification or regression.
In the case of regression, this involves calculating the average of the predictions from the models.
In the case of classification, the predictions for each label are summed and the label with the majority vote is predicted.
There are two approaches to the majority vote prediction for classification; they are hard voting and soft voting.
Hard voting involves summing the predictions for each class label and predicting the class label with the most votes.
Soft voting involves summing the predicted probabilities (or probability-like scores) for each class label and predicting the class label with the largest probability.
A voting ensemble may be considered a meta-model, a model of models.
As a meta-model, it could be used with any collection of existing trained machine learning models and the existing models do not need to be aware that they are being used in the ensemble.
This means you could explore using a voting ensemble on any set or subset of fit models for your predictive modeling task.
A voting ensemble is appropriate when you have two or more models that perform well on a predictive modeling task.
The models used in the ensemble must mostly agree with their predictions.
One way to combine outputs is by voting—the same mechanism used in bagging.
However, (unweighted) voting only makes sense if the learning schemes perform comparably well.
If two of the three classifiers make predictions that are grossly incorrect, we will be in trouble!— Page 497, Data Mining: Practical Machine Learning Tools and Techniques, 2016.
Use voting ensembles when:Hard voting is appropriate when the models used in the voting ensemble predict crisp class labels.
Soft voting is appropriate when the models used in the voting ensemble predict the probability of class membership.
Soft voting can be used for models that do not natively predict a class membership probability, although may require calibration of their probability-like scores prior to being used in the ensemble (e.
g.
support vector machine, k-nearest neighbors, and decision trees).
The voting ensemble is not guaranteed to provide better performance than any single model used in the ensemble.
If any given model used in the ensemble performs better than the voting ensemble, that model should probably be used instead of the voting ensemble.
This is not always the case.
A voting ensemble can offer lower variance in the predictions made over individual models.
This can be seen in a lower variance in prediction error for regression tasks.
This can also be seen in a lower variance in accuracy for classification tasks.
This lower variance may result in a lower mean performance of the ensemble, which might be desirable given the higher stability or confidence of the model.
Use a voting ensemble if:A voting ensemble is particularly useful for machine learning models that use a stochastic learning algorithm and result in a different final model each time it is trained on the same dataset.
One example is neural networks that are fit using stochastic gradient descent.
For more on this topic, see the tutorial:Another particularly useful case for voting ensembles is when combining multiple fits of the same machine learning algorithm with slightly different hyperparameters.
Voting ensembles are most effective when:A limitation of the voting ensemble is that it treats all models the same, meaning all models contribute equally to the prediction.
This is a problem if some models are good in some situations and poor in others.
An extension to the voting ensemble to address this problem is to use a weighted average or weighted voting of the contributing models.
This is sometimes called blending.
A further extension is to use a machine learning model to learn when and how much to trust each model when making predictions.
This is referred to as stacked generalization, or stacking for short.
Extensions to voting ensembles:Now that we are familiar with voting ensembles, let’s take a closer look at how to create voting ensemble models.
Voting ensembles can be implemented from scratch, although it can be challenging for beginners.
The scikit-learn Python machine learning library provides an implementation of voting for machine learning.
It is available in version 0.
22 of the library and higher.
First, confirm that you are using a modern version of the library by running the following script:Running the script will print your version of scikit-learn.
Your version should be the same or higher.
If not, you must upgrade your version of the scikit-learn library.
Voting is provided via the VotingRegressor and VotingClassifier classes.
Both models operate the same way and take the same arguments.
Using the model requires that you specify a list of estimators that make predictions and are combined in the voting ensemble.
A list of base models is provided via the “estimators” argument.
This is a Python list where each element in the list is a tuple with the name of the model and the configured model instance.
Each model in the list must have a unique name.
For example, below defines two base models:Each model in the list may also be a Pipeline, including any data preparation required by the model prior to fitting the model on the training dataset.
For example:When using a voting ensemble for classification, the type of voting, such as hard voting or soft voting, can be specified via the “voting” argument and set to the string ‘hard‘ (the default) or ‘soft‘.
For example:Now that we are familiar with the voting ensemble API in scikit-learn, let’s look at some worked examples.
In this section, we will look at using stacking for a classification problem.
First, we can use the make_classification() function to create a synthetic binary classification problem with 1,000 examples and 20 input features.
The complete example is listed below.
Running the example creates the dataset and summarizes the shape of the input and output components.
Next, we will demonstrate hard voting and soft voting for this dataset.
We can demonstrate hard voting with a k-nearest neighbor algorithm.
We can fit five different versions of the KNN algorithm, each with a different number of neighbors used when making predictions.
We will use 1, 3, 5, 7, and 9 neighbors (odd numbers in an attempt to avoid ties).
Our expectation is that by combining the predicted class labels predicted by each different KNN model that the hard voting ensemble will achieve a better predictive performance than any standalone model used in the ensemble, on average.
First, we can create a function named get_voting() that creates each KNN model and combines the models into a hard voting ensemble.
We can then create a list of models to evaluate, including each standalone version of the KNN model configurations and the hard voting ensemble.
This will help us directly compare each standalone configuration of the KNN model with the ensemble in terms of the distribution of classification accuracy scores.
The get_models() function below creates the list of models for us to evaluate.
Each model will be evaluated using repeated k-fold cross-validation.
The evaluate_model() function below takes a model instance and returns as a list of scores from three repeats of stratified 10-fold cross-validation.
We can then report the mean performance of each algorithm, and also create a box and whisker plot to compare the distribution of accuracy scores for each algorithm.
Tying this together, the complete example is listed below.
Running the example first reports the mean and standard deviation accuracy for each model.
We can see the hard voting ensemble achieves a better classification accuracy of about 90.
2% compared to all standalone versions of the model.
A box-and-whisker plot is then created comparing the distribution accuracy scores for each model, allowing us to clearly see that hard voting ensemble performing better than all standalone models on average.
Box Plot of Hard Voting Ensemble Compared to Standalone Models for Binary ClassificationIf we choose a hard voting ensemble as our final model, we can fit and use it to make predictions on new data just like any other model.
First, the hard voting ensemble is fit on all available data, then the predict() function can be called to make predictions on new data.
The example below demonstrates this on our binary classification dataset.
Running the example fits the hard voting ensemble model on the entire dataset and is then used to make a prediction on a new row of data, as we might when using the model in an application.
We can demonstrate soft voting with the support vector machine (SVM) algorithm.
The SVM algorithm does not natively predict probabilities, although it can be configured to predict probability-like scores by setting the “probability” argument to “True” in the SVC class.
We can fit five different versions of the SVM algorithm with a polynomial kernel, each with a different polynomial degree, set via the “degree” argument.
We will use degrees 1-5.
Our expectation is that by combining the predicted class membership probability scores predicted by each different SVM model that the soft voting ensemble will achieve a better predictive performance than any standalone model used in the ensemble, on average.
First, we can create a function named get_voting() that creates the SVM models and combines them into a soft voting ensemble.
We can then create a list of models to evaluate, including each standalone version of the SVM model configurations and the soft voting ensemble.
This will help us directly compare each standalone configuration of the SVM model with the ensemble in terms of the distribution of classification accuracy scores.
The get_models() function below creates the list of models for us to evaluate.
We can evaluate and report model performance using repeated k-fold cross-validation as we did in the previous section.
Tying this together, the complete example is listed below.
Running the example first reports the mean and standard deviation accuracy for each model.
We can see the soft voting ensemble achieves a better classification accuracy of about 92.
4% compared to all standalone versions of the model.
A box-and-whisker plot is then created comparing the distribution accuracy scores for each model, allowing us to clearly see that soft voting ensemble performing better than all standalone models on average.
Box Plot of Soft Voting Ensemble Compared to Standalone Models for Binary ClassificationIf we choose a soft voting ensemble as our final model, we can fit and use it to make predictions on new data just like any other model.
First, the soft voting ensemble is fit on all available data, then the predict() function can be called to make predictions on new data.
The example below demonstrates this on our binary classification dataset.
Running the example fits the soft voting ensemble model on the entire dataset and is then used to make a prediction on a new row of data, as we might when using the model in an application.
In this section, we will look at using voting for a regression problem.
First, we can use the make_regression() function to create a synthetic regression problem with 1,000 examples and 20 input features.
The complete example is listed below.
Running the example creates the dataset and summarizes the shape of the input and output components.
We can demonstrate ensemble voting for regression with a decision tree algorithm, sometimes referred to as a classification and regression tree (CART) algorithm.
We can fit five different versions of the CART algorithm, each with a different maximum depth of the decision tree, set via the “max_depth” argument.
We will use depths of 1-5.
Our expectation is that by combining the predicted class labels predicted by each different CART model that the voting ensemble will achieve a better predictive performance than any standalone model used in the ensemble, on average.
First, we can create a function named get_voting() that creates each CART model and combines the models into a voting ensemble.
We can then create a list of models to evaluate, including each standalone version of the CART model configurations and the soft voting ensemble.
This will help us directly compare each standalone configuration of the CART model with the ensemble in terms of the distribution of classification accuracy scores.
The get_models() function below creates the list of models for us to evaluate.
We can evaluate and report model performance using repeated k-fold cross-validation as we did in the previous section.
Models are evaluated using mean squared error (MSE).
The scikit-learn makes the score negative so that it can be maximized.
This means that the reported MSE scores are negative, larger values are better, and 0 represents no error.
Tying this together, the complete example is listed below.
Running the example first reports the mean and standard deviation accuracy for each model.
We can see the voting ensemble achieves a better mean squared error of about -136.
338, which is larger (better) compared to all standalone versions of the model.
A box-and-whisker plot is then created comparing the distribution negative MSE scores for each model, allowing us to clearly see that voting ensemble performing better than all standalone models on average.
Box Plot of Voting Ensemble Compared to Standalone Models for RegressionIf we choose a voting ensemble as our final model, we can fit and use it to make predictions on new data just like any other model.
First, the soft voting ensemble is fit on all available data, then the predict() function can be called to make predictions on new data.
The example below demonstrates this on our binary classification dataset.
Running the example fits the voting ensemble model on the entire dataset and is then used to make a prediction on a new row of data, as we might when using the model in an application.
This section provides more resources on the topic if you are looking to go deeper.
In this tutorial, you discovered how to create voting ensembles for machine learning algorithms in Python.
Specifically, you learned:Do you have any questions? Ask your questions in the comments below and I will do my best to answer.
with just a few lines of scikit-learn codeLearn how in my new Ebook: Machine Learning Mastery With PythonCovers self-study tutorials and end-to-end projects like: Loading data, visualization, modeling, tuning, and much more.
Skip the Academics.
Just Results.
.