AAPred.eval_selective

AAPred.eval_selective(X, labels, *, confidence=None, metrics=None, coverages=None, n_cv=5, label_pos=1)[source]

Measure the risk-coverage trade-off: performance as a function of the samples retained.

When a classifier may decline to score its least-confident samples, its accuracy on the ones it does score rises. This method quantifies that trade-off instead of leaving it to a guess: samples are ranked by a per-sample confidence signal, and every metric is scored again on the most-confident coverage fraction of them, for each level of a coverage grid. Reading “at 60% coverage the balanced accuracy is 0.93” off the returned table is the evidence base for choosing a refusal threshold.

This is a measurement only. Nothing abstains and no threshold is applied: acting on the curve (refusing, escalating) is decision logic that belongs to the caller. The ordinary eval() output is untouched by this method.

Scores come from the same cross-validated out-of-fold path as predict_oof(), so every sample is scored by models fit on the folds that exclude it and no prior fit() is needed. The coverage = 1.0 row therefore retains every sample and is the ordinary out-of-fold score of that metric.

Added in version 1.2.0.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Feature matrix cross-validated to produce the out-of-fold scores. Rows are samples, columns are features.

  • labels (array-like, shape (n_samples,)) – Class labels for samples in X; exactly two classes (typically 1 for the positive class and 0 for the negative one).

  • confidence (array-like, shape (n_samples,), optional) – Per-sample confidence used to rank the samples, higher meaning more confident, row-aligned with X. Any signal can be passed: the score margin, an uncertainty measure, or the applicability-domain distance of ReliabilityModel.predict() (negate a distance so that larger means more confident). None (default) ranks by the out-of-fold score margin |score - 0.5| * 2, which is 1 for a decisive score and 0 for a coin flip. Values must be finite; equally confident samples keep their input order.

  • metrics (list of str, optional) – Performance metrics scored at each coverage level, each one of 'accuracy', 'balanced_accuracy', 'precision', 'recall', 'f1', 'roc_auc' and 'mcc'. Defaults to list_metrics from the constructor.

  • coverages (list of int or float, optional) – Target coverage grid: the minimum fractions of samples to retain, each in (0, 1] and in strictly increasing order. Defaults to [0.2, 0.4, 0.6, 0.8, 1.0]. Level c retains the leading ceil(c * n_samples) samples of the confidence ranking, so a level is never empty and its actual retained fraction can be greater than c.

  • n_cv (int, default=5) – Number of stratified cross-validation folds behind the out-of-fold scores (must not exceed the smallest class count).

  • label_pos (int, default=1) – Label of the positive class whose out-of-fold probability is scored and thresholded at 0.5 for the hard-label metrics.

Returns:

df_eval_selective – Long-format risk-coverage table with columns metric, coverage (the fraction actually retained), n_retained (samples kept at that level), score (the metric on that retained subset) and score_aurc (the area under that metric’s coverage-performance curve divided by the actual coverage span, repeated on each of the metric’s rows, so a flat curve at 0.8 has an area of 0.8). score is NaN where a metric is undefined on the retained subset (balanced_accuracy and roc_auc need both classes present), and score_aurc is NaN for a single-level grid or a curve with a NaN point.

Return type:

pd.DataFrame, shape (n_metrics * n_coverages, 5)

See also

Examples

AAPred().eval_selective() measures the risk-coverage trade-off: how much a model’s performance improves when it only has to score its most-confident samples. Samples are ranked by a per-sample confidence signal, and every metric is scored again on the most-confident coverage fraction of them (see [Breimann25]). It is a measurement: nothing abstains, and choosing a refusal threshold from the curve stays with the caller. We first obtain the DOM_GSEC dataset and its feature matrix:

import aaanalysis as aa
aa.options["verbose"] = False  # Disable verbosity

# DOM_GSEC example dataset + its feature set (see [Breimann25]_)
df_seq = aa.load_dataset(name="DOM_GSEC")
labels = df_seq["label"].to_list()
df_feat = aa.load_features(name="DOM_GSEC").head(20)

# Build the CPP feature matrix
sf = aa.SequenceFeature()
df_parts = sf.get_df_parts(df_seq=df_seq)
X = sf.feature_matrix(features=df_feat["feature"], df_parts=df_parts)

eval_selective cross-validates the models given at construction (no prior :meth:fit is needed) and returns one row per metric and coverage level. score is the metric on the retained subset, n_retained the number of samples it kept, and score_aurc the area under that metric’s coverage-performance curve. The coverage = 1.0 row retains every sample, so it is the ordinary out-of-fold score:

aap = aa.AAPred(models=["rf", "extra_trees", "log_reg"], random_state=42)
df_eval_selective = aap.eval_selective(X, labels)
aa.display_df(df_eval_selective, n_rows=10, show_shape=True)
DataFrame shape: (20, 5)
  metric coverage n_retained score score_aurc
1 accuracy 0.206349 26 0.961538 0.897369
2 accuracy 0.404762 51 0.941176 0.897369
3 accuracy 0.603175 76 0.881579 0.897369
4 accuracy 0.801587 101 0.881188 0.897369
5 accuracy 1.000000 126 0.809524 0.897369
6 balanced_accuracy 0.206349 26 0.954545 0.898337
7 balanced_accuracy 0.404762 51 0.942742 0.898337
8 balanced_accuracy 0.603175 76 0.884778 0.898337
9 balanced_accuracy 0.801587 101 0.883794 0.898337
10 balanced_accuracy 1.000000 126 0.809524 0.898337

metrics selects which performance metrics are scored, and coverages sets the grid of retained fractions (each in (0, 1], strictly increasing). A finer grid traces the curve more closely:

df_eval_selective = aap.eval_selective(X, labels,
                                       metrics=["balanced_accuracy", "f1"],
                                       coverages=[0.25, 0.5, 0.75, 1.0])
aa.display_df(df_eval_selective, n_rows=10, show_shape=True)
DataFrame shape: (8, 5)
  metric coverage n_retained score score_aurc
1 balanced_accuracy 0.253968 32 0.933333 0.891652
2 balanced_accuracy 0.500000 63 0.907484 0.891652
3 balanced_accuracy 0.753968 95 0.895722 0.891652
4 balanced_accuracy 1.000000 126 0.809524 0.891652
5 f1 0.253968 32 0.916667 0.880873
6 f1 0.500000 63 0.888889 0.880873
7 f1 0.753968 95 0.888889 0.880873
8 f1 1.000000 126 0.812500 0.880873

confidence is the ranking signal and is fully pluggable: pass the score margin, an uncertainty measure, or an applicability-domain distance (negated, so that larger means more confident). Here the ensemble’s disagreement (score_std from :meth:AAPred.predict_oof) is negated, so samples the models agree on are retained first. Leaving confidence=None ranks by the out-of-fold score margin instead:

df_pred = aap.predict_oof(X, labels)
confidence = -df_pred["score_std"].to_numpy()  # low disagreement = high confidence
df_eval_selective = aap.eval_selective(X, labels,
                                       confidence=confidence,
                                       metrics=["accuracy"],
                                       coverages=[0.25, 0.5, 0.75, 1.0])
aa.display_df(df_eval_selective, n_rows=10, show_shape=True)
DataFrame shape: (4, 5)
  metric coverage n_retained score score_aurc
1 accuracy 0.253968 32 0.781250 0.821099
2 accuracy 0.500000 63 0.825397 0.821099
3 accuracy 0.753968 95 0.842105 0.821099
4 accuracy 1.000000 126 0.809524 0.821099

n_cv sets the number of stratified cross-validation folds behind the out-of-fold scores (it must not exceed the smallest class count), and label_pos names the class scored as positive:

df_eval_selective = aap.eval_selective(X, labels,
                                       metrics=["accuracy"],
                                       n_cv=3,
                                       label_pos=1)
aa.display_df(df_eval_selective, n_rows=10, show_shape=True)
DataFrame shape: (5, 5)
  metric coverage n_retained score score_aurc
1 accuracy 0.206349 26 0.923077 0.884192
2 accuracy 0.404762 51 0.921569 0.884192
3 accuracy 0.603175 76 0.881579 0.884192
4 accuracy 0.801587 101 0.871287 0.884192
5 accuracy 1.000000 126 0.801587 0.884192