Classification Accuracy

Beginner
~8 min
code completion

Accuracy Score

Accuracy is the simplest classification metric — the fraction of predictions that match the ground truth:

In NumPy:

np.mean(y_true == y_pred)

The == produces a boolean array; mean converts True→1, False→0 and averages.

Limitation: accuracy is misleading on imbalanced datasets (e.g., 99% negative class — predicting all-negative gives 99% accuracy but zero useful signal).

Your task:

Implement accuracy(y_true, y_pred) that returns the fraction of correct predictions.

Example Tests

3 correct out of 5

Input: {"y_pred":[1,0,0,1,1],"y_true":[1,0,1,1,0]}

Expected: 0.6

All correct: accuracy = 1.0

Input: {"y_pred":[1,1,0,0],"y_true":[1,1,0,0]}

Expected: 1

All wrong: accuracy = 0.0

Input: {"y_pred":[1,1,1],"y_true":[0,0,0]}

Expected: 0

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.