Skip to main content

imbcal

Calibrated probability estimation under class imbalance.

pip install imbcal

Rebalancing (class weights, undersampling, SMOTE) is the default answer to class imbalance, but it targets classification and damages the probabilities that risk prediction needs, even after recalibration. imbcal wraps any scikit-learn classifier with pipelines that estimate probabilities at the natural prevalence:

  • strategy="none" (recommended default): fit at the natural prevalence, then recalibrate (Platt or isotonic) on out-of-fold scores.
  • strategy="augment": prevalence-preserving augmentation. Synthetic minority points from any generator receive weights that keep the training prevalence unchanged. Real minority points get weight chi, and the m synthetic points get (1 - chi) n_min / m. The generator and chi are chosen by cross-validation of the recalibrated loss, and chi = 1 (no augmentation) is always a candidate, so the selection can fall back to the natural fit.
  • strategy="weights": balanced weights with recalibration, for comparison.
  • selection="safe": augment only if some candidate beats no augmentation with simultaneous significance (cross-validation with confidence and a max-T critical value), which controls the probability of choosing a harmful augmentation.

It also provides metrics (log-loss, Brier score, calibration slope and intercept, ECE), paired comparisons over repeated splits (Nadeau–Bengio corrected standard errors and a sign test), diagnostics for synthetic data, and a planning tool:

from imbcal import rebalancing_cost, print_rebalancing_cost
# predicted calibration slope, AUC and log-loss of maximum likelihood and of rebalanced
# logistic regressions, before any data are collected
print_rebalancing_cost(rebalancing_cost(p=36, prevalence=0.092, auc=0.81, epv=2.5))

Example

from sklearn.ensemble import RandomForestClassifier
from imbcal import ImbalanceCalibratedClassifier, GaussianGenerator, SMOTEGenerator, probability_report

model = ImbalanceCalibratedClassifier(
    RandomForestClassifier(min_samples_leaf=10),
    strategy="augment",
    generators=[GaussianGenerator(0.25), SMOTEGenerator(k="sqrt")],
    calibration="platt", selection="1se", random_state=0,
).fit(X_train, y_train)

print(model.selected_.label(), model.candidate_distance_)
print(probability_report(y_test, model.predict_proba(X_test)[:, 1]))

Any object with a sample(X_min, m, rng) method can be used as a generator, including wrappers around deep tabular generators.

Status

Alpha (0.3.0). Binary outcomes only; requires scikit-learn >= 1.6. The API may change before 1.0. Planned additions:

  • generators for mixed (continuous and categorical) features;
  • group-wise calibration diagnostics;
  • wrappers for deep tabular generators;
  • multiclass outcomes.

License

MIT.

Metadata

Release files for imbcal 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for imbcal 0.3.0
File Size Uploaded
imbcal-0.3.0.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for imbcal 0.3.0
File Interpreter ABI Platform
imbcal-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.5 kB

Release files / imbcal-0.3.0.tar.gz

Download URL imbcal-0.3.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cef6f1405068317ec9902686a14803e0b00ee93389c1ab6ae2bd84453f19fe30
BLAKE2b-256 checksum
How to use checksums
267b861b8f25e6a0c51196a30a63bb691ca4823c94e31c2428eeb5aada5241bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / imbcal-0.3.0-py3-none-any.whl

Download URL imbcal-0.3.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3310bc26700f4322eb9e519aac6bcea72db185892416f35616e06a56889b2a07
BLAKE2b-256 checksum
How to use checksums
3946af1c96a7fb6e838ff18f24577f4d06047ec62e1e204db18c346c5d6f8598
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page