PythiloLesson 8 of 12 · progress kept in this browser

Select an operating threshold using explicit error costs

A classifier's best operating threshold depends on the costs of false positives and false negatives, not on a universal value of one half. Choose the threshold on validation data after probability or ranking training has finished, then lock it before final testing.

This exercise evaluates a supplied candidate set and returns total cost. It deliberately uses a deterministic higher-threshold tie break. Real deployments should include capacity constraints, subgroup behavior and changes in prevalence. A threshold that minimizes historical validation cost can still fail when the outcome distribution or downstream cost changes.

labels=[0,1]
scores=[.6,.7]
threshold=.5
false_positives=sum(y==0 and s>=threshold for y,s in zip(labels,scores))
print(false_positives)
Your task

Implement cost_threshold(labels,scores,candidates,fp_cost,fn_cost). Predict 1 at score>=threshold. Return (threshold,total_cost), minimizing cost and choosing the higher threshold on ties. Validate nonempty equal data, binary labels, finite scores/candidates and finite nonnegative costs.