{"record":{"id":"4372f4554cf12762","repo":"rohitg00/ai-engineering-from-scratch","slug":"bins-must-be-positive","errorCode":null,"errorMessage":"bins must be positive","messagePattern":"bins must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/73-perplexity-calibration/code/main.py","lineNumber":83,"sourceCode":"        raise ValueError(\"confidences must lie in [0, 1]\")\n    uniq = set(np.unique(correct).tolist())\n    if not uniq.issubset({0, 1, 0.0, 1.0, True, False}):\n        raise ValueError(\"correct must be 0/1 or boolean\")\n\n\ndef _bin_indices(confidences: np.ndarray, n_bins: int) -> np.ndarray:\n    edges = np.linspace(0.0, 1.0, n_bins + 1)\n    idx = np.searchsorted(edges, confidences, side=\"right\") - 1\n    idx = np.clip(idx, 0, n_bins - 1)\n    return idx\n\n\ndef expected_calibration_error(confidences: Sequence[float], correct: Sequence[int], bins: int = 10) -> tuple[float, int]:\n    conf = np.asarray(confidences, dtype=np.float64)\n    corr = np.asarray(correct, dtype=np.float64)\n    _validate_probs(conf, corr)\n    if bins <= 0:\n        raise ValueError(\"bins must be positive\")\n    n = conf.size\n    if n == 0:\n        return (0.0, 0)\n    idx = _bin_indices(conf, bins)\n    total_gap = 0.0\n    populated = 0\n    for b in range(bins):\n        mask = idx == b\n        size = int(mask.sum())\n        if size == 0:\n            continue\n        populated += 1\n        avg_conf = float(conf[mask].mean())\n        avg_acc = float(corr[mask].mean())\n        total_gap += (size / n) * abs(avg_conf - avg_acc)\n    return (float(total_gap), populated)\n\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/73-perplexity-calibration/code/main.py#L65-L101","documentation":"Error \"bins must be positive\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/73-perplexity-calibration/code/main.py:83 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}