The Majority Baseline: What Doing Nothing Scores

Classic MLEasy+10 XP+10

The Majority Baseline: What Doing Nothing Scores

Before you trust any model, you need to know what doing nothing scores. The majority baseline is the laziest possible classifier: find the most common class in the training data, then predict that same class for everything, forever. It never looks at a single feature.

Task: write majority_baseline(y_train, y_test) returning [predicted_class, accuracy] — the class it would always predict, and the accuracy that gets on the test labels, rounded to 4 decimal places.

  • The predicted class is whichever label appears most often in y_train. If two labels tie, return the smaller one (labels are either all numbers or all strings, so they're always comparable).
  • Accuracy is measured on y_test: the fraction of test labels equal to that one predicted class.
  • The class is chosen from the training labels only. Peeking at the test labels to pick it would be cheating, and would also make the score meaningless.
  • Both lists are non-empty.

This number is the bar every real model has to clear. On a dataset where 99% of transactions are legitimate, the baseline scores 0.99 — so a fraud model reporting "99% accurate" has, in fact, demonstrated nothing at all.

Slide the Class Balance

The model still just predicts "Negative" every time. Watch what its Accuracy does.

Positive (1%)Negative (99%)
Model Accuracy
99%

Examples

y_train = [1, 1, 0, 1]
y_test = [1, 0, 1]
returns [1, 0.6667]
y_train = ['cat', 'dog', 'cat']
y_test = ['dog', 'dog']
returns ['cat', 0.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run