Conformity Scores¶
Conformity score classes for regression and classification.
Regression¶
mapie.conformity_scores.BaseRegressionScore
¶
Bases: BaseConformityScore
Base conformity score class for regression task.
This class should not be used directly. Use derived classes instead.
| PARAMETER | DESCRIPTION |
|---|---|
sym
|
Whether to consider the conformity score as symmetrical or not.
TYPE:
|
consistency_check
|
Whether to check the consistency between the methods
By default
TYPE:
|
eps
|
Threshold to consider when checking the consistency
between By default, it is defined by the default precision.
TYPE:
|
Source code in mapie/conformity_scores/regression.py
get_signed_conformity_scores
abstractmethod
¶
Placeholder for get_conformity_scores.
Subclasses should implement this method!
Compute the sample conformity scores given the predicted and observed targets.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Signed conformity scores. |
Source code in mapie/conformity_scores/regression.py
get_conformity_scores
¶
Get the conformity score considering the symmetrical property if so.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/regression.py
check_consistency
¶
Check consistency between the following methods:
get_estimation_distribution and get_signed_conformity_scores
The following equality should be verified::
y == self.get_estimation_distribution(
y_pred,
self.get_conformity_scores(y, y_pred, **kwargs),
**kwargs)
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
conformity_scores
|
Conformity scores.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the two methods are not consistent. |
Source code in mapie/conformity_scores/regression.py
get_estimation_distribution
abstractmethod
¶
Placeholder for get_estimation_distribution.
Subclasses should implement this method!
Compute samples of the estimation distribution given the predicted targets and the conformity scores.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred
|
Predicted target values.
TYPE:
|
conformity_scores
|
Conformity scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Observed values. |
Source code in mapie/conformity_scores/regression.py
get_bounds
¶
get_bounds(
X: NDArray,
alpha_np: NDArray,
estimator: EnsembleRegressor,
conformity_scores: NDArray,
ensemble: bool = False,
method: str = "base",
optimize_beta: bool = False,
allow_infinite_bounds: bool = False,
) -> Tuple[NDArray, NDArray, NDArray]
Compute bounds of the prediction intervals from the observed values,
the estimator of type EnsembleRegressor and the conformity scores.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values.
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
estimator
|
Estimator that is fitted to predict y from X.
TYPE:
|
conformity_scores
|
Conformity scores.
TYPE:
|
ensemble
|
Boolean determining whether the predictions are ensembled or not. By default
TYPE:
|
method
|
Method to choose for prediction interval estimates.
The By default
TYPE:
|
optimize_beta
|
Whether to optimize the PIs' width or not. By default
TYPE:
|
allow_infinite_bounds
|
Allow infinite prediction intervals to be produced. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[NDArray, NDArray, NDArray]
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If beta optimisation with symmetrical conformity score function. |
Source code in mapie/conformity_scores/regression.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
predict_set
¶
Compute the prediction sets on new samples based on the uncertainty of the target confidence set.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
The input data or samples for prediction.
TYPE:
|
alpha_np
|
Represents the uncertainty of the confidence set to produce.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
result
|
The prediction sets for each sample and each alpha level.
The output structure depends on the |
Source code in mapie/conformity_scores/regression.py
get_effective_calibration_samples
¶
Calculates the effective number of calibration samples.
| PARAMETER | DESCRIPTION |
|---|---|
scores
|
An array of scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
n
|
The effective number of calibration samples.
TYPE:
|
Source code in mapie/conformity_scores/regression.py
mapie.conformity_scores.AbsoluteConformityScore
¶
Bases: BaseRegressionScore
Absolute conformity score.
The signed conformity score = y - y_pred. The conformity score is symmetrical.
This is appropriate when the confidence interval is symmetrical and its range is approximatively the same over the range of predicted values.
References
[1] Lei, J., G'Sell, M., Rinaldo, A., Tibshirani, R. J. & Wasserman, L.. "Distribution-Free Predictive Inference for Regression." Journal of the American Statistical Association 2018.
Source code in mapie/conformity_scores/bounds/absolute.py
get_signed_conformity_scores
¶
Compute the signed conformity scores from the predicted values and the observed ones, from the following formula: signed conformity score = y - y_pred
Source code in mapie/conformity_scores/bounds/absolute.py
get_estimation_distribution
¶
get_estimation_distribution(
y_pred: ArrayLike,
conformity_scores: ArrayLike,
**kwargs,
) -> NDArray
Compute samples of the estimation distribution from the predicted values and the conformity scores, from the following formula: signed conformity score = y - y_pred <=> y = y_pred + signed conformity score
conformity_scores can be either the conformity scores or
the quantile of the conformity scores.
Source code in mapie/conformity_scores/bounds/absolute.py
mapie.conformity_scores.GammaConformityScore
¶
Bases: BaseRegressionScore
Gamma conformity score.
The signed conformity score = (y - y_pred) / y_pred. The conformity score is not symmetrical.
This is appropriate when the confidence interval is not symmetrical and its range depends on the predicted values. Like the Gamma distribution, its support is limited to strictly positive reals.
References
[1] Cordier, T., Blot, V., Lacombe, L., Morzadec, T., Capitaine, A. & Brunel, N.. "Flexible and Systematic Uncertainty Estimation with Conformal Prediction via the MAPIE library." Proceedings of Machine Learning Research 2023.
Source code in mapie/conformity_scores/bounds/gamma.py
get_signed_conformity_scores
¶
Compute the signed conformity scores from the observed values and the predicted ones, from the following formula: signed conformity score = (y - y_pred) / y_pred
Source code in mapie/conformity_scores/bounds/gamma.py
get_estimation_distribution
¶
get_estimation_distribution(
y_pred: ArrayLike,
conformity_scores: ArrayLike,
**kwargs,
) -> NDArray
Compute samples of the estimation distribution from the predicted values and the conformity scores, from the following formula: signed conformity score = (y - y_pred) / y_pred <=> y = y_pred * (1 + signed conformity score)
conformity_scores can be either the conformity scores or
the quantile of the conformity scores.
Source code in mapie/conformity_scores/bounds/gamma.py
mapie.conformity_scores.ResidualNormalisedScore
¶
ResidualNormalisedScore(
residual_estimator: Optional[RegressorMixin] = None,
prefit: bool = False,
split_size: Optional[Union[int, float]] = None,
random_state: Optional[Union[int, RandomState]] = None,
sym: bool = True,
consistency_check: bool = False,
)
Bases: BaseRegressionScore
Residual Normalised score.
The signed conformity score = abs(y - y_pred) / r_pred. r_pred being the predicted residual abs(y - y_pred) of the base estimator. It is calculated by a model that learns to predict these residuals. The learning is done with the log of the residual and we use the exponential of the prediction to avoid negative values.
The conformity score is symmetrical and allows the calculation of adaptive prediction intervals (taking X into account). It is possible to use it only with split and prefit methods (not with cross methods).
Warning : if the estimator provided is not fitted a subset of the calibration data will be used to fit the model (20% by default).
References
[1] Lei, J., G'Sell, M., Rinaldo, A., Tibshirani, R. J. & Wasserman, L.. "Distribution-Free Predictive Inference for Regression." Journal of the American Statistical Association 2018.
| PARAMETER | DESCRIPTION |
|---|---|
residual_estimator
|
The model that learns to predict the residuals of the base estimator.
It can be any regressor with scikit-learn API (i.e. with
TYPE:
|
prefit
|
Specify if the
TYPE:
|
split_size
|
The proportion of data that is used to fit the
TYPE:
|
random_state
|
Pseudo random number used for random sampling.
Pass an int for reproducible output across multiple function calls.
By default
TYPE:
|
Source code in mapie/conformity_scores/bounds/residuals.py
get_signed_conformity_scores
¶
get_signed_conformity_scores(
y: ArrayLike,
y_pred: ArrayLike,
X: Optional[ArrayLike] = None,
**kwargs,
) -> NDArray
Computes the signed conformity score = (y - y_pred) / r_pred.
r_pred being the predicted residual abs(y - y_pred) of the estimator.
It is calculated by a model (residual_estimator_) that learns
to predict this residual.
The learning is done with the log of the residual and later we use the exponential of the prediction to avoid negative values.
Source code in mapie/conformity_scores/bounds/residuals.py
get_estimation_distribution
¶
get_estimation_distribution(
y_pred: ArrayLike,
conformity_scores: ArrayLike,
X: Optional[ArrayLike] = None,
**kwargs,
) -> NDArray
Compute samples of the estimation distribution from the predicted
values and the conformity scores, from the following formula:
y_pred + conformity_scores * r_pred.
The learning has been done with the log of the residual so we use the exponential of the prediction to avoid negative values.
conformity_scores can be either the conformity scores or
the quantile of the conformity scores.
Source code in mapie/conformity_scores/bounds/residuals.py
Classification¶
mapie.conformity_scores.BaseClassificationScore
¶
Bases: BaseConformityScore
Base conformity score class for classification task.
This class should not be used directly. Use derived classes instead.
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
Source code in mapie/conformity_scores/classification.py
set_external_attributes
¶
set_external_attributes(
*,
classes: Optional[ArrayLike] = None,
random_state: Optional[Union[int, RandomState]] = None,
**kwargs,
) -> None
Set attributes that are not provided by the user.
| PARAMETER | DESCRIPTION |
|---|---|
classes
|
Names of the classes. By default
TYPE:
|
random_state
|
Pseudo random number generator state.
TYPE:
|
Source code in mapie/conformity_scores/classification.py
get_predictions
abstractmethod
¶
get_predictions(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Abstract method to get predictions from an EnsembleClassifier.
This method should be implemented by any subclass of the current class.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values.
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of predictions. |
Source code in mapie/conformity_scores/classification.py
get_conformity_score_quantiles
abstractmethod
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Abstract method to get quantiles of the conformity scores.
This method should be implemented by any subclass of the current class.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence set.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/classification.py
get_prediction_sets
abstractmethod
¶
get_prediction_sets(
y_pred_proba: NDArray,
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Abstract method to generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.
This method should be implemented by any subclass of the current class.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_proba
|
Target prediction.
TYPE:
|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence set.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/classification.py
get_sets
¶
get_sets(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
conformity_scores: NDArray,
**kwargs,
) -> NDArray
Compute classes of the prediction sets from the observed values, the predicted probabilities and the conformity scores.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence set.
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
conformity_scores
|
Conformity scores.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples, n_classes, n_alpha)
|
Prediction sets (Booleans indicate whether classes are included). |
Source code in mapie/conformity_scores/classification.py
predict_set
¶
Compute the prediction sets on new samples based on the uncertainty of the target confidence set.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
The input data or samples for prediction.
TYPE:
|
alpha_np
|
Represents the uncertainty of the confidence set to produce.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
result
|
The prediction sets for each sample and each alpha level.
The output structure depends on the |
Source code in mapie/conformity_scores/classification.py
mapie.conformity_scores.NaiveConformityScore
¶
Bases: BaseClassificationScore
Naive classification non-conformity score method that is based on the cumulative sum of probabilities until the 1-alpha threshold.
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
Source code in mapie/conformity_scores/sets/naive.py
get_conformity_scores
¶
Get the conformity score.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values (not used here).
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/sets/naive.py
get_predictions
¶
get_predictions(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Just processes the passed y_pred_proba.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values (not used since predictions are passed).
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of predictions. |
Source code in mapie/conformity_scores/sets/naive.py
get_conformity_score_quantiles
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Get the quantiles of the conformity scores for each uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample (not used here).
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval (not used here).
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/naive.py
get_prediction_sets
¶
get_prediction_sets(
y_pred_proba: NDArray,
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_proba
|
Target prediction.
TYPE:
|
conformity_scores
|
Conformity scores for each sample (not used here).
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval (not used here).
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/naive.py
mapie.conformity_scores.LACConformityScore
¶
Bases: BaseClassificationScore
Least Ambiguous set-valued Classifier (LAC) method-based
non conformity score (also formerly called "score").
It is based on the scores (i.e. 1 minus the softmax score of the true label) on the conformalization set.
References
[1] Mauricio Sadinle, Jing Lei, and Larry Wasserman. "Least Ambiguous Set-Valued Classifiers with Bounded Error Levels.", Journal of the American Statistical Association, 114, 2019.
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
Source code in mapie/conformity_scores/sets/lac.py
get_conformity_scores
¶
get_conformity_scores(
y: NDArray,
y_pred: NDArray,
y_enc: Optional[NDArray] = None,
**kwargs,
) -> NDArray
Get the conformity score.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values (not used here).
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
y_enc
|
Target values as normalized encodings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/sets/lac.py
get_predictions
¶
get_predictions(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
**kwargs,
) -> NDArray
Just processes the passed y_pred_proba.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values (not used since predictions are passed).
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of predictions. |
Source code in mapie/conformity_scores/sets/lac.py
get_conformity_score_quantiles
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
**kwargs,
) -> NDArray
Get the quantiles of the conformity scores for each uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/lac.py
get_prediction_sets
¶
get_prediction_sets(
y_pred_proba: NDArray,
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
**kwargs,
) -> NDArray
Generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_proba
|
Target prediction.
TYPE:
|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/lac.py
mapie.conformity_scores.APSConformityScore
¶
Bases: NaiveConformityScore
Adaptive Prediction Sets (APS) method-based non-conformity score. It is based on the sum of the softmax outputs of the labels until the true label is reached, on the conformalization set. See [1] for more details.
References
[1] Yaniv Romano, Matteo Sesia and Emmanuel J. Candès. "Classification with Valid and Adaptive Coverage." NeurIPS 202 (spotlight) 2020.
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
Source code in mapie/conformity_scores/sets/aps.py
get_predictions
¶
get_predictions(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
**kwargs,
) -> NDArray
Just processes the passed y_pred_proba.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values (not used since predictions are passed).
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of predictions. |
Source code in mapie/conformity_scores/sets/aps.py
get_true_label_cumsum_proba
staticmethod
¶
get_true_label_cumsum_proba(
y: ArrayLike, y_pred_proba: NDArray, classes: ArrayLike
) -> Tuple[NDArray, NDArray]
Compute the cumsumed probability of the true label.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Array with the labels.
TYPE:
|
y_pred_proba
|
Predictions of the model.
TYPE:
|
classes
|
Array with the classes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[NDArray, NDArray] of shapes (n_samples, 1) and (n_samples, ).
|
The first element is the cumsum probability of the true label. The second is the 1-based rank of the true label in the sorted probabilities. |
Source code in mapie/conformity_scores/sets/aps.py
get_conformity_scores
¶
get_conformity_scores(
y: NDArray,
y_pred: NDArray,
y_enc: Optional[NDArray] = None,
**kwargs,
) -> NDArray
Get the conformity score.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
y_enc
|
Target values as normalized encodings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/sets/aps.py
get_conformity_score_quantiles
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
**kwargs,
) -> NDArray
Get the quantiles of the conformity scores for each uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/aps.py
get_prediction_sets
¶
get_prediction_sets(
y_pred_proba: NDArray,
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
include_last_label: Optional[Union[bool, str]] = True,
**kwargs,
) -> NDArray
Generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_proba
|
Target prediction.
TYPE:
|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval (not used here).
TYPE:
|
cv
|
Cross-validation strategy used by the estimator.
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation. By default
TYPE:
|
include_last_label
|
Whether or not to include last label in prediction sets for the "aps" method. Choose among:
When set to By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
References
[1] Yaniv Romano, Matteo Sesia and Emmanuel J. Candès. "Classification with Valid and Adaptive Coverage." NeurIPS 202 (spotlight) 2020.
[2] Anastasios Nikolas Angelopoulos, Stephen Bates, Michael Jordan and Jitendra Malik. "Uncertainty Sets for Image Classifiers using Conformal Prediction." International Conference on Learning Representations 2021.
Source code in mapie/conformity_scores/sets/aps.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
mapie.conformity_scores.RAPSConformityScore
¶
Bases: APSConformityScore
Regularized Adaptive Prediction Sets (RAPS) method-based non-conformity
score. It uses the same technique as APSConformityScore class but with
a penalty term to reduce the size of prediction sets. See [1] for more
details. For now, this method only works with "prefit" and "split"
strategies.
References
[1] Anastasios Nikolas Angelopoulos, Stephen Bates, Michael Jordan and Jitendra Malik. "Uncertainty Sets for Image Classifiers using Conformal Prediction." International Conference on Learning Representations 2021.
| PARAMETER | DESCRIPTION |
|---|---|
size_raps
|
Percentage of the data to be used for choosing lambda_star and k_star for the RAPS method.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
label_encoder |
The label encoder used to encode the labels.
TYPE:
|
size_raps |
Percentage of the data to be used for choosing lambda_star and k_star for the RAPS method.
TYPE:
|
Source code in mapie/conformity_scores/sets/raps.py
set_external_attributes
¶
set_external_attributes(
*,
label_encoder: Optional[LabelEncoder] = None,
size_raps: Optional[float] = None,
**kwargs,
) -> None
Set attributes that are not provided by the user.
| PARAMETER | DESCRIPTION |
|---|---|
label_encoder
|
The label encoder used to encode the labels. By default
TYPE:
|
size_raps
|
Percentage of the data to be used for choosing lambda_star and k_star for the RAPS method. By default
TYPE:
|
Source code in mapie/conformity_scores/sets/raps.py
split_data
¶
split_data(
X: NDArray,
y: NDArray,
y_enc: NDArray,
sample_weight: Optional[NDArray] = None,
groups: Optional[NDArray] = None,
)
Split data. Keeps part of the data for the calibration estimator (separate from the calibration data).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed values.
TYPE:
|
y
|
Target values.
TYPE:
|
y_enc
|
Target values as normalized encodings.
TYPE:
|
sample_weight
|
Non-null sample weights.
TYPE:
|
groups
|
Group labels for the samples used while splitting the dataset into
train/test set.
By default
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[NDArray, NDArray, NDArray, NDArray, Optional[NDArray],
|
|
Optional[NDArray]]
|
|
Source code in mapie/conformity_scores/sets/raps.py
get_conformity_scores
¶
get_conformity_scores(
y: NDArray,
y_pred: NDArray,
y_enc: Optional[NDArray] = None,
**kwargs,
) -> NDArray
Get the conformity score.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
y_enc
|
Target values as normalized encodings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/sets/raps.py
get_conformity_score_quantiles
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
agg_scores: Optional[str] = "mean",
include_last_label: Optional[Union[bool, str]] = True,
**kwargs,
) -> NDArray
Get the quantiles of the conformity scores for each uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
agg_scores
|
Method to aggregate the scores from the base estimators. If "mean", the scores are averaged. If "crossval", the scores are obtained from cross-validation (not used here). By default,
TYPE:
|
include_last_label
|
Whether or not to include last label in prediction sets.
Choose among By default, See the docstring of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/raps.py
mapie.conformity_scores.TopKConformityScore
¶
Bases: BaseClassificationScore
Top-K method-based non-conformity score.
It is based on the sorted index of the probability of the true label in the softmax outputs, on the conformalization set. In case two probabilities are equal, both are taken, thus, the size of some prediction sets may be different from the others.
References
[1] Anastasios Nikolas Angelopoulos, Stephen Bates, Michael Jordan and Jitendra Malik. "Uncertainty Sets for Image Classifiers using Conformal Prediction." International Conference on Learning Representations 2021.
| ATTRIBUTE | DESCRIPTION |
|---|---|
classes |
Names of the classes.
TYPE:
|
random_state |
Pseudo random number generator state.
TYPE:
|
quantiles_ |
The quantiles estimated from
TYPE:
|
Source code in mapie/conformity_scores/sets/topk.py
get_conformity_scores
¶
get_conformity_scores(
y: NDArray,
y_pred: NDArray,
y_enc: Optional[NDArray] = None,
**kwargs,
) -> NDArray
Get the conformity score.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Observed target values.
TYPE:
|
y_pred
|
Predicted target values.
TYPE:
|
y_enc
|
Target values as normalized encodings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray of shape (n_samples,)
|
Conformity scores. |
Source code in mapie/conformity_scores/sets/topk.py
get_predictions
¶
get_predictions(
X: NDArray,
alpha_np: NDArray,
y_pred_proba: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Just processes the passed y_pred_proba.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Observed feature values (not used since predictions are passed).
TYPE:
|
alpha_np
|
NDArray of floats between
TYPE:
|
y_pred_proba
|
Predicted probabilities from the estimator.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of predictions. |
Source code in mapie/conformity_scores/sets/topk.py
get_conformity_score_quantiles
¶
get_conformity_score_quantiles(
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Get the quantiles of the conformity scores for each uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
conformity_scores
|
Conformity scores for each sample.
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |
Source code in mapie/conformity_scores/sets/topk.py
get_prediction_sets
¶
get_prediction_sets(
y_pred_proba: NDArray,
conformity_scores: NDArray,
alpha_np: NDArray,
cv: Optional[Union[int, str, BaseCrossValidator]],
**kwargs,
) -> NDArray
Generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.
| PARAMETER | DESCRIPTION |
|---|---|
y_pred_proba
|
Target prediction.
TYPE:
|
conformity_scores
|
Conformity scores for each sample (not used here).
TYPE:
|
alpha_np
|
NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval (not used here).
TYPE:
|
cv
|
Cross-validation strategy used by the estimator (not used here).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
Array of quantiles with respect to alpha_np. |