Precision Score

Easy
~12 min
code completion

Precision

When your classifier predicts "positive", how often is it right?

where:

  • TP (True Positive): predicted positive, actually positive
  • FP (False Positive): predicted positive, actually negative
  • High precision means: "When I say yes, I'm usually right."

    In NumPy:

    tp = np.sum((y_pred == 1) & (y_true == 1))
    fp = np.sum((y_pred == 1) & (y_true == 0))
    precision = tp / (tp + fp)

    Your task:

    Implement precision(y_true, y_pred). Assume at least one positive prediction exists.

    Example Tests

    2 TP, 1 FP: precision = 0.667

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

    Expected: 0.66667

    No false positives: precision = 1.0

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

    Expected: 1

    1 TP, 2 FP: precision = 0.333

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

    Expected: 0.33333

    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.