ML Grind

Accuracy from the Confusion Matrix - Model Evaluation

Implement Accuracy from the Confusion Matrix in Python with NumPy. easy exercise with examples, hints, and instant feedback. No setup or signup required.

Accuracy from the Confusion Matrix

What you'll build

accuracy(y_true, y_pred): the fraction of predictions that match the true labels, for binary or multi-class labels.

Background

The confusion matrix M of a C-class problem has M[a, p] = the number of samples with true class a that were predicted as class p. Correct predictions sit on its diagonal, so accuracy is the diagonal sum over the total. It is the first metric everyone reports and the most misleading one on imbalanced data: a model that always predicts "not fraud" on a 1%-fraud dataset scores 0.99. This is sklearn.metrics.accuracy_score.

Formula

accuracy = (1/n) * sum_i [y_pred[i] == y_true[i]]  =  trace(M) / n

For binary labels this is (TP + TN) / (TP + TN + FP + FN).

Inputs

  • y_true: true integer labels, shape (n,), n >= 1
  • y_pred: predicted integer labels, shape (n,)

(Tests may pass Python lists; convert with np.asarray.)

Expected return format

A float in [0, 1] (not rounded).

Edge cases and conventions

  • Works unchanged for any number of classes: only equality matters.
  • All correct gives 1.0; all wrong gives 0.0.

Constraints

  • NumPy only, vectorised (no loop over samples).

Follow-up

  • Build the full (C, C) confusion matrix with np.add.at or np.bincount(C * y_true + y_pred, minlength=C * C).
  • What is balanced accuracy, and when would you report it instead?

Examples

Input: accuracy(np.array([0, 1, 1, 0, 1]), np.array([0, 1, 0, 0, 1]))
Output: 0.8

4 of the 5 predictions match (index 2 is wrong). The confusion matrix is [[2, 0], [1, 2]] with trace 4.

Input: accuracy(np.array([0, 1, 2, 0, 1, 2]), np.array([0, 1, 2, 2, 1, 0]))
Output: 0.6666666666666666

Multi-class works the same way: 4 of 6 labels match.

Practice in Python

import numpy as np

def accuracy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Fraction of predictions equal to the true labels.

    Args:
        y_true: True integer labels, shape (n,)
        y_pred: Predicted integer labels, shape (n,)

    Returns:
        Accuracy, a float in [0, 1]
    """
    # Your code here
    pass

The interactive editor loads with Python and NumPy. Run your implementation against 5 tests, or use the progressive hints.

More exercises in this section →