Accuracy scoreEasynumpymetricsevaluationclassificationfundamentals
Accuracy score
Background
Accuracy is the simplest classification metric: the fraction of predictions that match the true labels. It is intuitive and a fine default for balanced problems, but misleading under class imbalance — a dataset that is 99% negative scores 99% accuracy by always predicting "negative". That failure mode is exactly why precision, recall, and F1 exist.
Problem statement
Implement accuracy(y_true, y_pred) returning the fraction of correct predictions:
Input
y_true— array-like of labels.y_pred— array-like of predicted labels, the same length.
Output
Returns a float in .
Examples
Example 1
Input: y_true = [0, 1, 1, 0, 1], y_pred = [0, 1, 0, 0, 1]
Output: 0.8
Explanation: 4 of the 5 predictions match (only index 2 differs), so accuracy .
Constraints
- Compare elementwise and return the mean of the matches as a
float. - Works for any label type, binary or multiclass.
- The result lies in .
Notes
- Accuracy treats every sample equally, so on imbalanced data it can be high while the minority class is ignored entirely.
- It equals the trace of the confusion matrix divided by the matrix's total.
Python
Loading...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example: 0.8
- •Perfect predictions -> 1.0
- •All wrong -> 0.0
- •Works for multiclass labels