EasyClassic ML

Implement Majority Class Classifier

Classic ML

Easy

Problem

Build a baseline classifier that predicts the most frequent training label for every test sample. If multiple labels have the same maximum frequency, choose the one that appears first in y_train. Features in X_test determine only how many predictions to return. Return a one-dimensional NumPy integer array.

Theory

A majority classifier (also called a "most frequent class" or "zero-rule" classifier) is the simplest possible classification model. It ignores all input features and always predicts the most common class in the training data.

\hat{y} = \arg\max_c \sum_{i=1}^{n} \mathbb{1}[y_i = c]

where \mathbb{1}[\cdot] is the indicator function that equals 1 when the condition is true.


Why Use a Majority Classifier?

1. Baseline performance

Any useful model should beat the majority classifier. If your model has 70% accuracy but the majority class represents 70% of the data, your model is not learning anything beyond the class distribution.

2. Sanity check

A quick way to verify your evaluation pipeline is working correctly.

3. Handling edge cases

When no features are available or all features are missing, fall back to the majority class.


Step-by-Step Process

Step 1: Count the occurrences of each class in the training data

Step 2: Identify the class with the highest count

Step 3: Store this class as the prediction

Step 4: For any new input, predict this stored class (ignoring input features)


Worked Example

Training data labels: [A, B, A, A, C, B, A, A, B, C]

Step 1: Count classes

Step 2: Find majority

Class A has the most occurrences (5 out of 10).

Step 3: Prediction rule

For any input x, predict class A.

Training accuracy: 5/10 = 50\%

This is the expected accuracy of the majority classifier on the training set.


Handling Ties

When two or more classes have the same highest count:

Option 1: Choose randomly among tied classes

Option 2: Choose the class with the smallest index or alphabetically first

Option 3: Use class priors if available

Example with tie:

Labels: [A, B, A, B, C, C]

All classes are tied. A deterministic implementation might choose A (first alphabetically).


Probability Estimates

A majority classifier can also output class probabilities based on training frequencies:

P(y = c) = \frac{\text{count}(c)}{n}

Example:

Labels: [A, A, A, B, B, C]

Prediction: Class A (highest probability)

Probability output: [0.5, 0.333, 0.167]


Expected Accuracy

The expected accuracy of a majority classifier equals the proportion of the majority class:

\text{Accuracy} = \frac{\text{count}(\text{majority class})}{n} = \max_c P(c)

Imbalanced datasets: If 90% of samples are class A, the majority classifier achieves 90% accuracy without learning anything useful.

Balanced datasets: If classes are evenly distributed, majority classifier accuracy equals 1/C where C is the number of classes.


Majority Classifier as a Baseline

Always compare your model against the majority classifier:

Scenario 1: Binary classification with 95% negative, 5% positive

Scenario 2: 3-class problem with equal distribution

Scenario 3: Your model underperforms majority classifier


Relationship to Other Models

Decision stump: A one-level decision tree that makes one split. Majority classifier is a decision stump with no splits (just the root).

Naive Bayes with uniform likelihood: If all features have the same distribution across classes, Naive Bayes reduces to the majority classifier.

Prior classifier: The majority classifier essentially uses only the class prior P(y) and ignores the likelihood P(x|y).


Multi-label Extension

For multi-label classification (where each sample can have multiple labels):

Per-label majority: For each label, predict 1 if that label appears in more than 50% of training samples, else 0.

Example:

Sample labels:

Label frequencies:


Computational Complexity

Training:

Prediction:

The majority classifier is extremely efficient in both training and inference.


When Majority Classifier Performs Well

Highly imbalanced data: When one class dominates (>90%), majority classifier is hard to beat without careful modeling.

No signal in features: If features are random noise, majority classifier is optimal.

Early stopping in trees: Decision trees often have majority classifier nodes as leaves.


Limitations

Ignores all features: Cannot adapt to different inputs

Blind to minority classes: Never predicts rare classes

Poor for balanced problems: Only achieves 1/C accuracy when classes are equally distributed

Not useful for decision making: Provides no insight into why a prediction was made

Examples

Example 1

Input
y_train = [0, 1, 1, 1, 0], X_test = [10, 20, 30]
Output
[1, 1, 1]
Explanation
Label 1 occurs three times, so it is predicted for all three samples.

Example 2

Input
y_train = [2, 2, 2, 1, 0], X_test = [5, 6]
Output
[2, 2]

Hints

  1. Use np.unique(y_train, return_index=True, return_counts=True) to obtain counts and first positions.
  2. Among labels with the maximum count, select the smallest recorded first position.

Requirements

Constraints

Starter Code

import numpy as np

def majority_classifier(y_train: list, X_test: list) -> np.ndarray:
    """
    Returns a one-dimensional NumPy array.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basic majorityExample 1public
Class 2 majorityExample 2public