Back to Practice
#0117

Binary Confusion Matrix With NumPy

MediumNumPy25 min20 XP

Problem

Given y_true and y_pred arrays containing 0/1 labels, return [tn, fp, fn, tp].

Why This Matters

Boolean masks are the NumPy way to express metric logic clearly and efficiently.

Function Signature

def binary_confusion_numpy(y_true, y_pred):

Examples

Example 1
Inputy_true=[1,0,1,0], y_pred=[1,1,0,0]
Output[1,1,1,1]

There is one of each outcome type.

Constraints

  • Return the requested value exactly.
  • Handle the stated edge cases.
  • Keep the solution readable.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputy_true=[1,0,1,0], y_pred=[1,1,0,0]
Expected[1,1,1,1]

There is one of each outcome type.

Hidden Test Categories
all correctall predicted positiveall predicted negative