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.
Cet exercice fait partie du cours
Ensemble Methods in Python
Instructions
- Prepare the list of
(string, estimator)tuples. Use'dt'as the label forclf_dt,'lr'forclf_lr, and'knn'forclf_knn. - Build a voting classifier called
clf_vote. - Build an averaging classifier called
clf_avg.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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))