concept

Threshold and class imbalance

Scores in [0, 1] at or above threshold become positive. Higher thresholds weakly reduce predicted positives; select on validation data with costs and class support in view. Teaching helper, not a library API.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
classify(scores, threshold)

Working Example

Example Snippetartificial-intelligence
def classify(scores, threshold):    if not 0 <= threshold <= 1:        raise ValueError("Threshold outside [0, 1]")    if any(not 0 <= s <= 1 for s in scores):        raise ValueError("Score outside [0, 1]")    return [int(s >= threshold) for s in scores]
scores = [0.9, 0.6, 0.4, 0.2]for t in (0.5, 0.7, 0.4):    print(t, classify(scores, t))