Classification: Majority Voting Practice Problem
This data science coding problem helps you practice Bagging Ensembles, classification: majority voting, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Bagging Ensembles.
- Problem ID: 213
- Problem key: 213-classification-majority-voting
- URL: https://datacrack.app/solve/213-classification-majority-voting
- Difficulty: easy
- Topic: Bagging Ensembles
- Module: Supervised Learning
Problem Statement
# 🧩 Classification: Majority Voting
---
### 🎯 Goal
Aggregate classification predictions from multiple models using majority voting.
---
### 📖 Introduction
Problem 1 showed how bootstrap sampling creates different versions of the training data.
After that, bagging usually trains one base model on each bootstrap sample. For classification, each trained model predicts a class label, and the ensemble combines those labels by voting.
For example:
```python
["cat", "dog", "cat"]
```
means three models voted:
- model 1 voted `cat`
- model 2 voted `dog`
- model 3 voted `cat`
The class `cat` gets 2 votes, so the final result should return:
```python
(2, "cat")
```
The first value is the winning class count. The second value is the winning class label.
If there is a tie, return the alphabetically smaller label. This keeps the result deterministic.
This problem focuses only on the aggregation step for classification. It assumes the models already made their predictions. Random Forests will later apply this idea using decision trees and random feature selection.
---
### 💻 Task
Implement `bagging_classification_vote(model_predictions)`.
Your function should:
- Count how many times each class label appears.
- Find the label with the most votes.
- If labels tie, choose the alphabetically smaller label.
- Return the winning class count and winning class label.
- Return `(0, None)` when there are no predictions.
---
### 📥 Input / 📤 Output
**Input:** a list of predictions from multiple models.
**Output:** two returned values:
```python
winning_class_count, winning_class
```
---
### 🧩 Starter Code
```python
def bagging_classification_vote(model_predictions):
# Your code here
return winning_class_count, winning_class
```
---
### 💡 Example
```python
bagging_classification_vote(["cat", "dog", "cat"])
```
Expected output:
```python
(2, "cat")
```
Tie example:
```python
bagging_classification_vote(["dog", "cat", "dog", "cat"])
```
Expected output:
```python
(2, "cat")
```
---
### ⚠️ Common Mistakes
- Returning only the class label without the winning count.
- Returning vote counts for every class instead of the two final outputs.
- Returning the first model's prediction instead of voting.
- Making tie-breaking random.
- Choosing the alphabetically larger label in a tie.
Starter Code
def bagging_classification_vote(model_predictions):
# Your code here
return winning_class_count, winning_class