Go to the end
to download the full example code. or to run this example in your browser via JupyterLite or Binder
Visualizing the probabilistic predictions of a VotingClassifier#
Plot the predicted class probabilities in a toy dataset predicted by three
different classifiers and averaged by the VotingClassifier.
First, three linear classifiers are initialized. Two are spline models with
interaction terms, one using constant extrapolation and the other using periodic
extrapolation. The third classifier is a Nystroem
with the default "rbf" kernel.
In the first part of this example, these three classifiers are used to
demonstrate soft-voting using VotingClassifier with weighted
average. We set weights=[2,1,3], meaning the constant extrapolation spline
model’s predictions are weighted twice as much as the periodic spline model’s,
and the Nystroem model’s predictions are weighted three times as much as the
periodic spline.
The second part demonstrates how soft predictions can be converted into hard
predictions.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
We first generate a noisy XOR dataset, which is a binary classification task.
Due to the inherent non-linear separability of the XOR dataset, tree-based
models would often be preferred. However, appropriate feature engineering
combined with a linear model can yield effective results, with the added
benefit of producing better-calibrated probabilities for samples located in
the transition regions affected by noise.
We define and fit the models on the whole dataset.
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Finally we use DecisionBoundaryDisplay to plot the
predicted probabilities. By using a diverging colormap (such as "RdBu"), we
can ensure that darker colors correspond to predict_proba close to either 0
or 1, and white corresponds to predict_proba of 0.5.
Splines with constant extrapolation, Splines with periodic extrapolation, RBF Nystroem, Soft Voting
As a sanity check, we can verify for a given sample that the probability
predicted by the VotingClassifier is indeed the weighted
average of the individual classifiers’ soft-predictions.
In the case of binary classification such as in the present example, the
predict_proba arrays contain the probability of belonging to class 0
(here in red) as the first entry, and the probability of belonging to class 1
(here in blue) as the second entry.
print("Weighted average of soft-predictions: "f"{np.dot(weights,predict_probas)/np.sum(weights)}")
Weighted average of soft-predictions: [0.3630784 0.6369216]
We can see that manual calculation of predicted probabilities above is
equivalent to that produced by the VotingClassifier:
print("Predicted probability of VotingClassifier: "f"{eclf.predict_proba(test_sample).ravel()}")
Predicted probability of VotingClassifier: [0.3630784 0.6369216]
To convert soft predictions into hard predictions when weights are provided,
the weighted average predicted probabilities are computed for each class.
Then, the final class label is then derived from the class label with the
highest average probability, which corresponds to the default threshold at
predict_proba=0.5 in the case of binary classification.
print("Class with the highest weighted average of soft-predictions: "f"{np.argmax(np.dot(weights,predict_probas)/np.sum(weights))}")
Class with the highest weighted average of soft-predictions: 1
This is equivalent to the output of VotingClassifier’s predict method:
print(f"Predicted class of VotingClassifier: {eclf.predict(test_sample).ravel()}")
Predicted class of VotingClassifier: [1]
Soft votes can be thresholded as for any other probabilistic classifier. This
allows you to set a threshold probability at which the positive class will be
predicted, instead of simply selecting the class with the highest predicted
probability.
fromsklearn.model_selectionimportFixedThresholdClassifiereclf_other_threshold=FixedThresholdClassifier(eclf,threshold=0.7,response_method="predict_proba").fit(X,y)print("Predicted class of thresholded VotingClassifier: "f"{eclf_other_threshold.predict(test_sample)}")
Predicted class of thresholded VotingClassifier: [0]
Total running time of the script: (0 minutes 0.667 seconds)