Classification Accuracy

~8 mincode completion

Implement that returns the fraction of correct predictions.

Examples

3 correct out of 5

Input
accuracy([1, 0, 1, 1, 0], [1, 0, 0, 1, 1])
Output
0.6
Input
accuracy([1, 1, 0, 0], [1, 1, 0, 0])
Output
1
Input
accuracy([0, 0, 0], [1, 1, 1])
Output
0

Hints

Hint 1

does the sum and the division in one step.

Hint 2

Watch for this: counted sum not mean.

Requirements

  • y_true: Ground truth labels, shape (m,)

  • y_pred: Predicted labels, shape (m,)

  • Return Fraction of correct predictions in [0, 1].

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

8 employers weight this skill

3 AI product companies, 2 frontier labs, 1 health and bio company, 1 big tech firm, 1 autonomy company. Top match scores 93.

Python
import numpy as np

def accuracy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Compute classification accuracy.

    Args:
        y_true: Ground truth labels, shape (m,)
        y_pred: Predicted labels, shape (m,)

    Returns:
        Fraction of correct predictions in [0, 1].
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.