Metadata-Version: 2.4
Name: calibration-proba
Version: 0.1.1
Summary: Probability calibration for binary and multiclass models with a scikit-learn style API.
Author: Maksim Tyurikov
License-Expression: MIT
Project-URL: Repository, https://github.com/maks371/calibration-proba
Keywords: probability-calibration,calibration,binary-classification,multiclass-classification,machine-learning
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21
Requires-Dist: scipy>=1.12
Requires-Dist: matplotlib>=3.6
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Dynamic: license-file

# calibration_proba

Calibrate predicted probabilities for binary and multiclass classifiers with
`ProbCalib`.

- **Binary** (`task="binary"`)
  - `isotonic_linear` (default)
  - `isotonic`
  - `sigmoid`
  - `temperature_scaling`
- **Multiclass** (`task="multiclass"`)
  - `temperature_scaling` (default)
  - `sigmoid`
  - `isotonic_linear`

[How the calibration methods work](https://github.com/maks371/calibration-proba/blob/main/docs/methods.md)

## Installation

```bash
pip install calibration-proba
```

## Calibrate probabilities

Fit the calibrator on a separate calibration set, then apply it to the test
predictions. This binary example uses the predicted probability of class 1.

```python
from calibration_proba import ProbCalib

p_cal = model.predict_proba(X_cal)[:, 1]
p_test = model.predict_proba(X_test)[:, 1]

calib = ProbCalib(task="binary", method="isotonic_linear", max_bins=10)
calib.fit(p_cal, y_cal)
p_calibrated = calib.predict(p_test)

calib.save_model("calibrator.npz")
calib = ProbCalib.load_model("calibrator.npz")
```

## Plot the mapping and reliability diagram

```python
from calibration_proba import plot_calibration_curve

mapping = calib.plot()
reliability = plot_calibration_curve(y_test, p_calibrated)

mapping.savefig("mapping.png", dpi=180)
reliability.savefig("reliability.png", dpi=180)
```

| Fitted calibration mapping | Reliability diagram |
| --- | --- |
| [![Fitted calibration mapping](https://raw.githubusercontent.com/maks371/calibration-proba/main/examples/binary_mapping_isotonic_linear.png)](https://raw.githubusercontent.com/maks371/calibration-proba/main/examples/binary_mapping_isotonic_linear.png) | [![Reliability diagram](https://raw.githubusercontent.com/maks371/calibration-proba/main/examples/binary_reliability.png)](https://raw.githubusercontent.com/maks371/calibration-proba/main/examples/binary_reliability.png) |

## Benchmark on Adult with CatBoost

ECE before calibration was **0.0098**. The table shows changes on the test
set relative to the uncalibrated predictions. Both isotonic methods use
`max_bins=None`. See the [full benchmark report](https://github.com/maks371/calibration-proba/blob/main/experiments/calibration_benchmark/results.md#adult).

| Method | Δ ECE | Δ ECE (%) | Δ ROC AUC |
| --- | ---: | ---: | ---: |
| `isotonic_linear` | **-0.0047** | -47.75% | 0 |
| `isotonic` | -0.0041 | -42.04% | -0.00035 |
| `sigmoid` | -0.0002 | -2.51% | 0 |
| `temperature_scaling` | +0.0008 | +7.92% | 0 |
