4
4
======================================================
5
5
6
6
In this tutorial, we compare the prediction sets estimated by
7
- :class:`~mapie .classification._MapieClassifier ` with the "lac"
7
+ :class:`~mapie_v1 .classification.SplitConformalClassifier ` with the "lac"
8
8
and "aps" on the two-dimensional dataset presented
9
9
by Sadinle et al. (2019).
10
10
"""
20
20
# classifier as the conformity score on a toy two-dimensional dataset.
21
21
# We estimate the prediction sets as follows :
22
22
#
23
- # * First we generate a dataset with train, calibration and test, the model
23
+ # * First we generate a dataset with train, conformalization and test, the model
24
24
# is fitted in the training set.
25
25
#
26
26
# * We set the conformal score ``Sᵢ = 𝑓̂(Xᵢ)ᵧᵢ``
27
27
# from the softmax output of the true class or the cumulated score
28
- # (by decreasing order) for each sample in the calibration set.
28
+ # (by decreasing order) for each sample in the conformalization set.
29
29
#
30
30
# * Then we define q̂ as being the
31
31
# ``(n + 1)(1 - α) / n``
54
54
from sklearn .naive_bayes import GaussianNB
55
55
56
56
from numpy .typing import NDArray
57
- from mapie .classification import _MapieClassifier
57
+ from mapie .classification import SplitConformalClassifier
58
58
from mapie .metrics .classification import (
59
- classification_coverage_score ,
59
+ classification_coverage_score_v2 ,
60
60
classification_mean_width_score ,
61
61
)
62
62
73
73
]
74
74
)
75
75
y = np .hstack ([np .full (n_samples , i ) for i in range (n_classes )])
76
- X_train , X_cal , y_train , y_cal = train_test_split (X , y , test_size = 0.3 )
76
+ X_train , X_conf , y_train , y_conf = train_test_split (X , y , test_size = 0.3 )
77
77
78
78
xx , yy = np .meshgrid (
79
79
np .arange (x_min , x_max , step ), np .arange (x_min , x_max , step )
102
102
103
103
##############################################################################
104
104
# We fit our training data with a Gaussian Naive Base estimator.
105
- # Then we apply :class:`~mapie.classification._MapieClassifier` in the
106
- # calibration data with the methods ``"lac"`` and ``"aps"```
107
- # to the estimator indicating that it has already been fitted with
108
- # `cv="prefit"`.
109
- # We then estimate the prediction sets with differents alpha values with a
110
- # ``fit`` and ``predict`` process.
111
-
112
- clf = GaussianNB ().fit (X_train , y_train )
105
+ # Then, we initialize a :class:`~mapie_v1.classification.SplitConformalClassifier` with
106
+ # conformity scores ``"lac"`` and ``"aps"`` , using our pre-fitted estimator.
107
+ # Lastly, we compute the prediction sets with different alpha values using the
108
+ # ``conformalize`` and ``predict`` methods.
109
+
110
+ clf = GaussianNB ()
111
+ clf .fit (X_train , y_train )
113
112
y_pred = clf .predict (X_test )
114
113
y_pred_proba = clf .predict_proba (X_test )
115
114
y_pred_proba_max = np .max (y_pred_proba , axis = 1 )
116
115
117
- methods = ["lac" , "aps" ]
116
+ conformity_scores = ["lac" , "aps" ]
118
117
mapie , y_pred_mapie , y_ps_mapie = {}, {}, {}
119
118
alpha = [0.2 , 0.1 , 0.05 ]
120
- for method in methods :
121
- mapie [method ] = _MapieClassifier (
119
+ for conformity_score in conformity_scores :
120
+ mapie [conformity_score ] = SplitConformalClassifier (
122
121
estimator = clf ,
123
- method = method ,
124
- cv = "prefit" ,
122
+ confidence_level = 1 - np .array (alpha ),
123
+ conformity_score = conformity_score ,
124
+ prefit = True ,
125
125
random_state = 42 ,
126
126
)
127
- mapie [method ].fit (X_cal , y_cal )
128
- y_pred_mapie [method ], y_ps_mapie [method ] = mapie [method ].predict (
129
- X_test , alpha = alpha , include_last_label = True ,
127
+ mapie [conformity_score ].conformalize (X_conf , y_conf )
128
+ y_pred_mapie [conformity_score ], y_ps_mapie [conformity_score ] = (
129
+ mapie [conformity_score ].predict_set (
130
+ X_test , conformity_score_params = {"include_last_label" : True }
131
+ )
130
132
)
131
133
132
134
@@ -146,7 +148,7 @@ def plot_scores(
146
148
alphas : List [float ],
147
149
scores : NDArray ,
148
150
quantiles : NDArray ,
149
- method : str ,
151
+ conformity_score : str ,
150
152
ax : plt .Axes ,
151
153
) -> None :
152
154
colors = {0 : "#1f77b4" , 1 : "#ff7f0e" , 2 : "#2ca02c" }
@@ -162,18 +164,19 @@ def plot_scores(
162
164
label = f"alpha = { alphas [i ]} " ,
163
165
)
164
166
i = i + 1
165
- ax .set_title (f"Distribution of scores for '{ method } ' method" )
167
+ ax .set_title (f"Distribution of scores for '{ conformity_score } ' method" )
166
168
ax .legend ()
167
169
ax .set_xlabel ("scores" )
168
170
ax .set_ylabel ("count" )
169
171
170
172
171
173
fig , axs = plt .subplots (1 , 2 , figsize = (10 , 5 ))
172
- for i , method in enumerate (methods ):
173
- conformity_scores = mapie [method ].conformity_scores_
174
- n = mapie [method ].n_samples_
175
- quantiles = mapie [method ].conformity_score_function_ .quantiles_
176
- plot_scores (alpha , conformity_scores , quantiles , method , axs [i ])
174
+ for i , conformity_score in enumerate (conformity_scores ):
175
+ conf_scores = mapie [conformity_score ]._mapie_classifier .conformity_scores_
176
+ n = mapie [conformity_score ]._mapie_classifier .n_samples_
177
+ quantiles = (
178
+ mapie [conformity_score ]._mapie_classifier .conformity_score_function_ .quantiles_ )
179
+ plot_scores (alpha , conf_scores , quantiles , conformity_score , axs [i ])
177
180
plt .show ()
178
181
179
182
@@ -228,8 +231,8 @@ def plot_results(
228
231
plt .show ()
229
232
230
233
231
- for method in methods :
232
- plot_results (alpha , y_pred_mapie [method ], y_ps_mapie [method ])
234
+ for conformity_score in conformity_scores :
235
+ plot_results (alpha , y_pred_mapie [conformity_score ], y_ps_mapie [conformity_score ])
233
236
234
237
235
238
##############################################################################
@@ -248,38 +251,41 @@ def plot_results(
248
251
alpha_ = np .arange (0.02 , 0.98 , 0.02 )
249
252
coverage , mean_width = {}, {}
250
253
mapie , y_ps_mapie = {}, {}
251
- for method in methods :
252
- mapie [method ] = _MapieClassifier (
254
+ for conformity_score in conformity_scores :
255
+ mapie [conformity_score ] = SplitConformalClassifier (
253
256
estimator = clf ,
254
- method = method ,
255
- cv = "prefit" ,
257
+ confidence_level = 1 - alpha_ ,
258
+ conformity_score = conformity_score ,
259
+ prefit = True ,
256
260
random_state = 42 ,
257
261
)
258
- mapie [method ]. fit ( X_cal , y_cal )
259
- _ , y_ps_mapie [method ] = mapie [method ]. predict (
260
- X , alpha = alpha_ , include_last_label = " randomized"
262
+ mapie [conformity_score ]. conformalize ( X_conf , y_conf )
263
+ _ , y_ps_mapie [conformity_score ] = mapie [conformity_score ]. predict_set (
264
+ X , conformity_score_params = { " include_last_label" : " randomized"}
261
265
)
262
- coverage [method ] = [
263
- classification_coverage_score (y , y_ps_mapie [method ][:, :, i ])
264
- for i , _ in enumerate (alpha_ )
266
+ coverage [conformity_score ] = [
267
+ classification_coverage_score_v2 (y , y_ps_mapie [conformity_score ])
265
268
]
266
- mean_width [method ] = classification_mean_width_score (y_ps_mapie [method ])
269
+ mean_width [conformity_score ] = classification_mean_width_score (
270
+ y_ps_mapie [conformity_score ]
271
+ )
267
272
268
273
fig , axs = plt .subplots (1 , 3 , figsize = (15 , 5 ))
269
274
axs [0 ].set_xlabel ("1 - alpha" )
270
275
axs [0 ].set_ylabel ("Quantile" )
271
- for method in methods :
272
- quantiles = mapie [method ].conformity_score_function_ .quantiles_
273
- axs [0 ].scatter (1 - alpha_ , quantiles , label = method )
276
+ for conformity_score in conformity_scores :
277
+ quantiles = (
278
+ mapie [conformity_score ]._mapie_classifier .conformity_score_function_ .quantiles_ )
279
+ axs [0 ].scatter (1 - alpha_ , quantiles , label = conformity_score )
274
280
axs [0 ].legend ()
275
- for method in methods :
276
- axs [1 ].scatter (1 - alpha_ , coverage [method ], label = method )
281
+ for conformity_score in conformity_scores :
282
+ axs [1 ].scatter (1 - alpha_ , coverage [conformity_score ], label = conformity_score )
277
283
axs [1 ].set_xlabel ("1 - alpha" )
278
284
axs [1 ].set_ylabel ("Coverage score" )
279
285
axs [1 ].plot ([0 , 1 ], [0 , 1 ], label = "x=y" , color = "black" )
280
286
axs [1 ].legend ()
281
- for method in methods :
282
- axs [2 ].scatter (1 - alpha_ , mean_width [method ], label = method )
287
+ for conformity_score in conformity_scores :
288
+ axs [2 ].scatter (1 - alpha_ , mean_width [conformity_score ], label = conformity_score )
283
289
axs [2 ].set_xlabel ("1 - alpha" )
284
290
axs [2 ].set_ylabel ("Average size of prediction sets" )
285
291
axs [2 ].legend ()
0 commit comments