Better performance with a Voting Classifier

Finally, you'll evaluate the performance of a voting classifier that takes the outputs of the models defined in the list classifiers and assigns labels by majority voting.

X_train, X_test,y_train, y_test, the list classifiers defined in a previous exercise, as well as the function accuracy_score from sklearn.metrics are available in your workspace.

This exercise is part of the course

Machine Learning with Tree-Based Models in Python

View Course

Exercise instructions

  • Import VotingClassifier from sklearn.ensemble.
  • Instantiate a VotingClassifier by setting the parameter estimators to classifiers and assign it to vc.
  • Fit vc to the training set.
  • Evaluate vc's test set accuracy using the test set predictions y_pred.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import VotingClassifier from sklearn.ensemble
____

# Instantiate a VotingClassifier vc
vc = ____(estimators=____)     

# Fit vc to the training set
____.____(____, ____)   

# Evaluate the test set predictions
y_pred = vc.predict(X_test)

# Calculate accuracy score
accuracy = ____(____, ____)
print('Voting Classifier: {:.3f}'.format(accuracy))