Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Ensemble Methods in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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))
Code bewerken en uitvoeren