Problems
Loading...
1 / 1

Implement Majority Class Classifier

Classic ML
Easy

Implement a majority class classifier that predicts the most frequent label in the training data for all test samples.

The majority classifier is the simplest possible baseline model in machine learning. Despite its simplicity, it provides a crucial performance benchmark that more sophisticated models should exceed. It's particularly useful for understanding dataset characteristics and class imbalance.

Function Arguments

  • y_train: array-like - Training labels (integers)
  • X_test: array-like - Test features (any shape, ignored for prediction)
Loading visualization...

Examples

Input: y_train=[0,1,1,1,0], X_test=[10,20,30]

Output: [1,1,1]

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

Output: [2,2]

Hint 1

Use np.unique() with return_counts=True to find class frequencies.

Hint 2

Use np.argmax() to find the index of the most frequent class.

Hint 3

Use np.full() to create an array filled with the majority class.

Requirements

  • Return numpy array of predictions with integer dtype
  • Predict the most frequent class from training data
  • Handle ties by choosing the first occurring class (stable behavior)
  • Support multi-class problems (not just binary)
  • Handle edge cases: empty test set, single training sample

Constraints

  • Training samples ≤ 1e6; NumPy only
  • Time limit: 100ms; Memory: 64MB
Try Similar Problems