Catch the Most, Stay Believable

Machine Learning in CodeMedium+10 XP+10

Catch the Most, Stay Believable

A bank's review team will only trust the fraud alerts if enough of them are real. Their rule: at least min_precision of the flagged payments must be fraud. Within that rule, they want to catch as much fraud as possible.

Task: write best_threshold(y_true, chance, thresholds, min_precision).

  • 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.
  • Try every threshold in thresholds. Keep only the ones whose precision is at least min_precision. Precision is 0.0 when nothing is flagged.
  • Among those, choose the one with the highest recall. If several tie on recall, choose the largest threshold among them.

Return [threshold, precision, recall], with precision and recall rounded to 3 decimal places. Compare the unrounded values when you choose.

In every test at least one threshold meets the rule.

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]
min_precision = 0.5
returns [0.3, 0.5, 0.75]
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]
min_precision = 0.75
returns [0.9, 1.0, 0.25]

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