Fill the Four Boxes

Machine Learning in CodeEasy+10 XP+10

Fill the Four Boxes

A spam filter has been tested on a handful of emails. y_true holds what each email really was and y_pred holds what the filter said. In both lists, 1 means spam (the positive class) and 0 means not spam.

Task: write four_boxes(y_true, y_pred) and return a dict with four whole numbers:

keycounts the emails that were…
"tn"really not spam, called not spam
"fp"really not spam, called spam
"fn"really spam, called not spam
"tp"really spam, called spam

Both 0 and 1 appear in y_true. The two lists always have the same length.

Examples

y_true = [0, 0, 1, 1, 0, 1]
y_pred = [0, 1, 1, 0, 0, 1]
returns {'tn': 2, 'fp': 1, 'fn': 1, 'tp': 2}
y_true = [1, 0, 1, 0, 0, 0, 1, 0]
y_pred = [0, 0, 0, 0, 0, 0, 0, 0]
returns {'tn': 5, 'fp': 0, 'fn': 3, 'tp': 0}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run