Get startedGet started for free

Soft vs. hard voting

You've now practiced building two types of ensemble methods: Voting and Averaging (soft voting). Which one is better? It's best to try both of them and then compare their performance. Let's try this now using the Game of Thrones dataset.

Three individual classifiers have been instantiated for you:

  • A DecisionTreeClassifier (clf_dt).
  • A LogisticRegression (clf_lr).
  • A KNeighborsClassifier (clf_knn).

Your task is to try both voting and averaging to determine which is better.

This exercise is part of the course

Ensemble Methods in Python

View Course

Exercise instructions

  • Prepare the list of (string, estimator) tuples. Use 'dt' as the label for clf_dt, 'lr' for clf_lr, and 'knn' for clf_knn.
  • Build a voting classifier called clf_vote.
  • Build an averaging classifier called clf_avg.

Hands-on interactive exercise

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

# List of (string, estimator) tuples
estimators = ____

# Build and fit a voting classifier
clf_vote = ____
clf_vote.fit(X_train, y_train)

# Build and fit an averaging classifier
clf_avg = ____
clf_avg.fit(X_train, y_train)

# Evaluate the performance of both models
acc_vote = accuracy_score(y_test, clf_vote.predict(X_test))
acc_avg = accuracy_score(y_test,  clf_avg.predict(X_test))
print('Voting: {:.2f}, Averaging: {:.2f}'.format(acc_vote, acc_avg))
Edit and Run Code