classifier.WeightedConformalClassifier
classifier.WeightedConformalClassifier(
model=None,
alpha=0.1,
nonconformity_function=None,
random_state=None,
)Weighted Conformal Classifier for handling covariate shift.
This classifier provides valid coverage guarantees when the test distribution P_test differs from the calibration distribution P_cal. It uses importance weights w_i = P_test(x_i) / P_cal(x_i) to reweight the calibration scores.
This is particularly useful when: - Deploying to a different population than the training data - Adapting to temporal distribution shifts - Handling domain adaptation scenarios
Parameters
model : Any = None-
A fitted classification model with a
predict_probamethod. alpha : float = 0.1-
Significance level. Target coverage is 1 - alpha.
nonconformity_function : Callable = None-
Function to compute nonconformity scores. Signature: func(y_true, y_prob) -> scores. If None, uses LAC (1 - P(y_true)).
random_state : int = None-
Random state for reproducibility.
Attributes
classes_ : ndarray-
Unique class labels.
n_classes_ : int-
Number of classes.
qhat_ : float-
The weighted calibration quantile threshold.
calibration_scores_ : ndarray-
Nonconformity scores from calibration.
calibration_weights_ : ndarray-
Normalized weights used during calibration.
Examples
>>> from sklearn.ensemble import RandomForestClassifier
>>> from conformalpy.classifier import WeightedConformalClassifier
>>> from conformalpy.nonconformity import lac_nonconformity
>>>
>>> # Train a classifier
>>> rf = RandomForestClassifier(n_estimators=100, random_state=42)
>>> rf.fit(X_train, y_train)
>>>
>>> # Create weighted conformal classifier
>>> wcc = WeightedConformalClassifier(
... model=rf,
... alpha=0.1,
... nonconformity_function=lac_nonconformity,
... )
>>>
>>> # Estimate importance weights (e.g., using density ratio estimation)
>>> # weights[i] = P_test(x_i) / P_cal(x_i)
>>> weights = estimate_importance_weights(X_calib, X_test)
>>>
>>> # Calibrate with weights
>>> wcc.calibrate(X_calib, y_calib, weights=weights)
>>>
>>> # Predict
>>> pred_sets = wcc.predict(X_test)References
Tibshirani et al. (2019). “Conformal Prediction Under Covariate Shift.” NeurIPS.
See Also
ConformalClassifier : Standard conformal classifier without weighting.
Methods
| Name | Description |
|---|---|
| calibrate | Calibrate the weighted conformal classifier. |
| fit | Fit (calibrate) the weighted conformal classifier. |
| get_params | Get parameters for this estimator. |
| predict | Predict conformal prediction sets. |
| predict_top_k | Predict top-K prediction sets. |
| score | Compute empirical coverage on test data. |
| set_params | Set parameters for this estimator. |
calibrate
classifier.WeightedConformalClassifier.calibrate(
X_calib,
y_calib,
*,
weights=None,
)Calibrate the weighted conformal classifier.
Parameters
X_calib : array-like of shape (n_samples, n_features)-
Calibration features.
y_calib : array-like of shape (n_samples,)-
Calibration labels.
weights : array-like of shape (n_samples,) = None-
Importance weights w_i = P_test(x_i) / P_cal(x_i). Should estimate the density ratio between test and calibration distributions. If None, uses uniform weights (standard CP).
Returns
: ndarray-
Nonconformity scores for calibration set.
Notes
The weights should satisfy: - Non-negative: w_i >= 0 - Ideally sum close to n (but will be normalized) - Represent P_test(x) / P_cal(x) for covariate shift handling
Methods for estimating weights include: - Kernel density estimation - Logistic regression (classify cal vs test) - Domain adaptation techniques
fit
classifier.WeightedConformalClassifier.fit(X_calib, y_calib, *, weights=None)Fit (calibrate) the weighted conformal classifier.
This is an alias for calibrate() to follow scikit-learn conventions.
Parameters
X_calib : array-like of shape (n_samples, n_features)-
Calibration features.
y_calib : array-like of shape (n_samples,)-
Calibration labels.
weights : array-like of shape (n_samples,) = None-
Importance weights for each calibration sample. If None, uniform weights are used (standard CP).
Returns
: self
get_params
classifier.WeightedConformalClassifier.get_params(deep=True)Get parameters for this estimator.
predict
classifier.WeightedConformalClassifier.predict(X, *, alpha=None)Predict conformal prediction sets.
Parameters
X : array-like of shape (n_samples, n_features)-
Input features.
alpha : float, list of float, or None = None-
Significance level(s). If None, uses self.alpha. If scalar, returns list[list[int]]. If array-like, returns (n_samples, n_classes, n_alphas) bool array.
Returns
: list of list of int or ndarray-
Prediction sets for each sample.
predict_top_k
classifier.WeightedConformalClassifier.predict_top_k(X, k)Predict top-K prediction sets.
Parameters
X : array-like of shape (n_samples, n_features)-
Input features.
k : int-
Number of classes to include.
Returns
: list of list of int-
Prediction sets with exactly k classes each.
score
classifier.WeightedConformalClassifier.score(X, y)Compute empirical coverage on test data.
Parameters
X : array-like of shape (n_samples, n_features)-
Test features.
y : array-like of shape (n_samples,)-
True labels.
Returns
: float-
Empirical coverage.
set_params
classifier.WeightedConformalClassifier.set_params(**params)Set parameters for this estimator.