Where Do You Draw the Line?

Machine Learning in CodeEasy+10 XP+10

Where Do You Draw the Line?

A fraud model gives every payment a chance of being fraud. The bank flags a payment when its chance is at or above a threshold it chooses.

Task: write flag_and_score(y_true, chance, threshold) and return [precision, recall], each rounded to 3 decimal places.

  • y_true holds 1 for fraud and 0 for a normal payment. It contains at least one 1.
  • chance holds the model's chance of fraud for each payment, in the same order.
  • Precision: of the flagged payments, the share that really were fraud.
  • Recall: of the real frauds, the share that were flagged.
  • If nothing is flagged, precision is 0.0.

Try a high and a low threshold on the same payments and watch the two scores pull against each other.

Examples

y_true = [1, 0, 1, 0, 0, 1, 0, 0]
chance = [0.9, 0.6, 0.4, 0.2, 0.1, 0.7, 0.3, 0.05]
threshold = 0.5
returns [0.667, 0.667]
y_true = [1, 0, 1, 0, 0, 1, 0, 0]
chance = [0.9, 0.6, 0.4, 0.2, 0.1, 0.7, 0.3, 0.05]
threshold = 0.25
returns [0.6, 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