mapie.conformity_scores.APSConformityScore

class mapie.conformity_scores.APSConformityScore[source]

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.

Attributes
classes: Optional[ArrayLike]

Names of the classes.

random_state: Optional[Union[int, RandomState]]

Pseudo random number generator state.

quantiles_: ArrayLike of shape (n_alpha)

The quantiles estimated from get_sets method.

__init__() None[source]
get_conformity_score_quantiles(conformity_scores: ndarray[Any, dtype[_ScalarType_co]], alpha_np: ndarray[Any, dtype[_ScalarType_co]], cv: Optional[Union[int, str, BaseCrossValidator]], agg_scores: Optional[str] = 'mean', **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Get the quantiles of the conformity scores for each uncertainty level.

Parameters
conformity_scores: NDArray of shape (n_samples,)

Conformity scores for each sample.

alpha_np: NDArray of shape (n_alpha,)

NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.

cv: Optional[Union[int, str, BaseCrossValidator]]

Cross-validation strategy used by the estimator.

agg_scores: Optional[str]

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 "mean".

Returns
NDArray

Array of quantiles with respect to alpha_np.

get_conformity_scores(y: ndarray[Any, dtype[_ScalarType_co]], y_pred: ndarray[Any, dtype[_ScalarType_co]], y_enc: Optional[ndarray[Any, dtype[_ScalarType_co]]] = None, **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Get the conformity score.

Parameters
y: NDArray of shape (n_samples,)

Observed target values.

y_pred: NDArray of shape (n_samples,)

Predicted target values.

y_enc: NDArray of shape (n_samples,)

Target values as normalized encodings.

Returns
NDArray of shape (n_samples,)

Conformity scores.

get_prediction_sets(y_pred_proba: ndarray[Any, dtype[_ScalarType_co]], conformity_scores: ndarray[Any, dtype[_ScalarType_co]], alpha_np: ndarray[Any, dtype[_ScalarType_co]], cv: Optional[Union[int, str, BaseCrossValidator]], agg_scores: Optional[str] = 'mean', include_last_label: Optional[Union[bool, str]] = True, **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Generate prediction sets based on the probability predictions, the conformity scores and the uncertainty level.

Parameters
y_pred_proba: NDArray of shape (n_samples, n_classes)

Target prediction.

conformity_scores: NDArray of shape (n_samples,)

Conformity scores for each sample.

alpha_np: NDArray of shape (n_alpha,)

NDArray of floats between 0 and 1, representing the uncertainty of the confidence interval.

cv: Optional[Union[int, str, BaseCrossValidator]]

Cross-validation strategy used by the estimator.

agg_scores: Optional[str]

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 "mean".

include_last_label: Optional[Union[bool, str]]

Whether or not to include last label in prediction sets for the “aps” method. Choose among:

  • False, does not include label whose cumulated score is just over the quantile.

  • True, includes label whose cumulated score is just over the quantile, unless there is only one label in the prediction set.

  • “randomized”, randomly includes label whose cumulated score is just over the quantile based on the comparison of a uniform number and the difference between the cumulated score of the last label and the quantile.

When set to True or False, it may result in a coverage higher than 1 - alpha (because contrary to the “randomized” setting, none of these methods create empty prediction sets). See [1] and [2] for more details.

By default True.

Returns
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.

get_predictions(X: ndarray[Any, dtype[_ScalarType_co]], alpha_np: ndarray[Any, dtype[_ScalarType_co]], y_pred_proba: ndarray[Any, dtype[_ScalarType_co]], cv: Optional[Union[int, str, BaseCrossValidator]], agg_scores: Optional[str] = 'mean', **kwargs) ndarray[Any, dtype[_ScalarType_co]][source]

Just processes the passed y_pred_proba.

Parameters
X: NDArray of shape (n_samples, n_features)

Observed feature values (not used since predictions are passed).

alpha_np: NDArray of shape (n_alpha,)

NDArray of floats between 0 and 1, represents the uncertainty of the confidence interval.

y_pred_proba: NDArray

Predicted probabilities from the estimator.

cv: Optional[Union[int, str, BaseCrossValidator]]

Cross-validation strategy used by the estimator.

agg_scores: Optional[str]

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 "mean".

Returns
NDArray

Array of predictions.

static get_true_label_cumsum_proba(y: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]], y_pred_proba: ndarray[Any, dtype[_ScalarType_co]], classes: Union[_SupportsArray[dtype[Any]], _NestedSequence[_SupportsArray[dtype[Any]]], bool, int, float, complex, str, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]]) Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]][source]

Compute the cumsumed probability of the true label.

Parameters
y: NDArray of shape (n_samples, )

Array with the labels.

y_pred_proba: NDArray of shape (n_samples, n_classes)

Predictions of the model.

classes: NDArray of shape (n_classes, )

Array with the classes.

Returns
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 sorted position of the true label.