Put a Price on Every Mistake

Machine Learning in CodeMedium+10 XP+10

Put a Price on Every Mistake

A bank has put a price on each kind of mistake. A missed fraud costs miss_cost. A false alarm, a good payment blocked by mistake, costs alarm_cost. Now the threshold can be chosen by money instead of by guesswork.

Task: write cheapest_threshold(y_true, chance, thresholds, miss_cost, alarm_cost).

  • y_true holds 1 for fraud and 0 otherwise. chance holds the model's chance of fraud for each row.
  • At a threshold t, a row is flagged when its chance is at or above t.
  • At each threshold in thresholds, the total cost is misses × miss_cost + false alarms × alarm_cost.
  • Choose the threshold with the lowest total cost. If several tie, choose the largest of them.

Return [threshold, total_cost, recall, precision] for the threshold you chose. Recall and precision are rounded to 3 decimal places, and precision is 0.0 if nothing is flagged. Both costs are whole numbers, so the total cost is a whole number too.

Examples

y_true = [1, 0, 1, 0, 0, 1, 0, 0, 1, 0]
chance = [0.92, 0.7, 0.66, 0.4, 0.35, 0.3, 0.2, 0.15, 0.12, 0.05]
thresholds = [0.9, 0.6, 0.3, 0.1]
miss_cost = 10
alarm_cost = 1
returns [0.1, 5, 1.0, 0.444]
y_true = [1, 0, 1, 0, 0, 1, 0, 0, 1, 0]
chance = [0.92, 0.7, 0.66, 0.4, 0.35, 0.3, 0.2, 0.15, 0.12, 0.05]
thresholds = [0.9, 0.6, 0.3, 0.1]
miss_cost = 1
alarm_cost = 1
returns [0.9, 3, 0.25, 1.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