15
15
from sklearn .pipeline import Pipeline , make_pipeline
16
16
from sklearn .preprocessing import OneHotEncoder
17
17
18
- from mapie .calibration import MapieCalibrator
18
+ from mapie .calibration import TopLabelCalibrator
19
19
from mapie .metrics .calibration import top_label_ece
20
20
from mapie .metrics .calibration import expected_calibration_error
21
21
110
110
111
111
def test_initialized () -> None :
112
112
"""Test that initialization does not crash."""
113
- MapieCalibrator ()
113
+ TopLabelCalibrator ()
114
114
115
115
116
116
def test_default_parameters () -> None :
117
117
"""Test default values of input parameters."""
118
- mapie_cal = MapieCalibrator ()
119
- assert mapie_cal .method == "top_label"
118
+ mapie_cal = TopLabelCalibrator ()
120
119
assert mapie_cal .calibrator is None
121
120
assert mapie_cal .cv == "split"
122
121
123
122
124
123
def test_default_fit_params () -> None :
125
124
"""Test default sample weights and other parameters."""
126
- mapie_cal = MapieCalibrator ()
125
+ mapie_cal = TopLabelCalibrator ()
127
126
assert (
128
127
signature (mapie_cal .fit ).parameters ["sample_weight" ].default
129
128
is None
@@ -152,15 +151,15 @@ def test_false_str_estimator() -> None:
152
151
ValueError ,
153
152
match = r".*Please provide a string in*" ,
154
153
):
155
- mapie_cal = MapieCalibrator (
154
+ mapie_cal = TopLabelCalibrator (
156
155
calibrator = "not_estimator"
157
156
)
158
157
mapie_cal .fit (X , y )
159
158
160
159
161
160
def test_estimator_none () -> None :
162
161
"""Test that no input for calibrator will return a sigmoid"""
163
- mapie_cal = MapieCalibrator ()
162
+ mapie_cal = TopLabelCalibrator ()
164
163
mapie_cal .fit (X , y )
165
164
assert isinstance (
166
165
mapie_cal .calibrators [list (mapie_cal .calibrators .keys ())[0 ]],
@@ -172,34 +171,24 @@ def test_check_type_of_target() -> None:
172
171
"""Test the type of target."""
173
172
X = [0.5 , 0.2 , 0.4 , 0.8 , 3.8 ]
174
173
y = [0.4 , 0.2 , 3.6 , 3 , 0.2 ]
175
- mapie_cal = MapieCalibrator ()
174
+ mapie_cal = TopLabelCalibrator ()
176
175
with pytest .raises (
177
176
ValueError ,
178
177
match = r".*Make sure to have one of the allowed targets:*"
179
178
):
180
179
mapie_cal .fit (X , y )
181
180
182
181
183
- def test_other_methods () -> None :
184
- """Test that invalid string for method returns error"""
185
- with pytest .raises (
186
- ValueError ,
187
- match = r".*Invalid method, allowed method are*" ,
188
- ):
189
- mapie_cal = MapieCalibrator (method = "no_method" )
190
- mapie_cal .fit (X , y )
191
-
192
-
193
182
def test_prefit_cv_argument () -> None :
194
183
"""Test that prefit method works"""
195
184
est = RandomForestClassifier ().fit (X , y )
196
- mapie_cal = MapieCalibrator (estimator = est , cv = "prefit" )
185
+ mapie_cal = TopLabelCalibrator (estimator = est , cv = "prefit" )
197
186
mapie_cal .fit (X , y )
198
187
199
188
200
189
def test_split_cv_argument () -> None :
201
190
"""Test that split method works"""
202
- mapie_cal = MapieCalibrator (cv = "split" )
191
+ mapie_cal = TopLabelCalibrator (cv = "split" )
203
192
mapie_cal .fit (X , y )
204
193
205
194
@@ -210,7 +199,7 @@ def test_invalid_cv_argument(cv: str) -> None:
210
199
ValueError ,
211
200
match = r".*Invalid cv argument*" ,
212
201
):
213
- mapie_cal = MapieCalibrator (cv = cv )
202
+ mapie_cal = TopLabelCalibrator (cv = cv )
214
203
mapie_cal .fit (X , y )
215
204
216
205
@@ -219,10 +208,10 @@ def test_prefit_split_same_results() -> None:
219
208
est = RandomForestClassifier (
220
209
random_state = random_state
221
210
).fit (X_train , y_train )
222
- mapie_cal_prefit = MapieCalibrator (estimator = est , cv = "prefit" )
211
+ mapie_cal_prefit = TopLabelCalibrator (estimator = est , cv = "prefit" )
223
212
mapie_cal_prefit .fit (X_calib , y_calib )
224
213
225
- mapie_cal_split = MapieCalibrator (
214
+ mapie_cal_split = TopLabelCalibrator (
226
215
estimator = RandomForestClassifier (random_state = random_state )
227
216
)
228
217
mapie_cal_split .fit (
@@ -242,7 +231,7 @@ def test_not_seen_calibrator() -> None:
242
231
UserWarning ,
243
232
match = r".*WARNING: This predicted label*"
244
233
):
245
- mapie_cal = MapieCalibrator ()
234
+ mapie_cal = TopLabelCalibrator ()
246
235
mapie_cal .fit (X , y )
247
236
mapie_cal .calibrators .clear ()
248
237
mapie_cal .predict_proba (X )
@@ -255,7 +244,7 @@ def test_shape_of_output(
255
244
estimator : ClassifierMixin
256
245
) -> None :
257
246
"""Test that the size of the outputs are coherent."""
258
- mapie_cal = MapieCalibrator (
247
+ mapie_cal = TopLabelCalibrator (
259
248
estimator = estimator ,
260
249
calibrator = calibrator ,
261
250
)
@@ -269,7 +258,7 @@ def test_number_of_classes_equal_calibrators() -> None:
269
258
Test that the number of calibrators is the same as the number
270
259
of classes in the calibration step.
271
260
"""
272
- mapie_cal = MapieCalibrator ()
261
+ mapie_cal = TopLabelCalibrator ()
273
262
mapie_cal .fit (
274
263
X = X_ ,
275
264
y = y_ ,
@@ -281,7 +270,7 @@ def test_number_of_classes_equal_calibrators() -> None:
281
270
282
271
def test_same_predict () -> None :
283
272
"""Test that the same prediction is made regardless of the calibration."""
284
- mapie_cal = MapieCalibrator ( method = "top_label" )
273
+ mapie_cal = TopLabelCalibrator ( )
285
274
mapie_cal .fit (
286
275
X = X_ ,
287
276
y = y_ ,
@@ -300,13 +289,13 @@ def test_same_predict() -> None:
300
289
)
301
290
302
291
303
- @pytest .mark .parametrize ("cv" , MapieCalibrator .valid_cv )
292
+ @pytest .mark .parametrize ("cv" , TopLabelCalibrator .valid_cv )
304
293
def test_correct_results (cv : str ) -> None :
305
294
"""
306
295
Test that the y_score and top label score from the test dataset result
307
296
in the correct scores (in a multi-class setting).
308
297
"""
309
- mapie_cal = MapieCalibrator (cv = cv )
298
+ mapie_cal = TopLabelCalibrator (cv = cv )
310
299
mapie_cal .fit (
311
300
X = X_ ,
312
301
y = y_ ,
@@ -326,7 +315,7 @@ def test_correct_results(cv: str) -> None:
326
315
)
327
316
328
317
329
- @pytest .mark .parametrize ("cv" , MapieCalibrator .valid_cv )
318
+ @pytest .mark .parametrize ("cv" , TopLabelCalibrator .valid_cv )
330
319
def test_correct_results_binary (cv : str ) -> None :
331
320
"""
332
321
Test that the y_score and top label score from the test dataset result
@@ -338,7 +327,7 @@ def test_correct_results_binary(cv: str) -> None:
338
327
n_informative = 4 ,
339
328
random_state = random_state
340
329
)
341
- mapie_cal = MapieCalibrator (cv = cv )
330
+ mapie_cal = TopLabelCalibrator (cv = cv )
342
331
mapie_cal .fit (
343
332
X = X_binary ,
344
333
y = y_binary ,
@@ -375,17 +364,17 @@ def test_different_binary_y_combinations() -> None:
375
364
n_informative = 4 ,
376
365
random_state = random_state
377
366
)
378
- mapie_cal = MapieCalibrator ()
367
+ mapie_cal = TopLabelCalibrator ()
379
368
mapie_cal .fit (X_comb , y_comb , random_state = random_state )
380
369
y_score = mapie_cal .predict_proba (X_comb )
381
370
382
371
y_comb1 = np .where (y_comb == 2 , 3 , y_comb )
383
- mapie_cal1 = MapieCalibrator ()
372
+ mapie_cal1 = TopLabelCalibrator ()
384
373
mapie_cal1 .fit (X_comb , y_comb1 , random_state = random_state )
385
374
y_score1 = mapie_cal1 .predict_proba (X_comb )
386
375
387
376
y_comb2 = np .where (y_comb == 2 , 40 , y_comb )
388
- mapie_cal2 = MapieCalibrator ()
377
+ mapie_cal2 = TopLabelCalibrator ()
389
378
mapie_cal2 .fit (X_comb , y_comb2 , random_state = random_state )
390
379
y_score2 = mapie_cal2 .predict_proba (X_comb )
391
380
np .testing .assert_array_almost_equal (y_score , y_score1 )
@@ -417,9 +406,9 @@ def test_results_with_constant_sample_weights(
417
406
"""
418
407
n_samples = len (X )
419
408
estimator = RandomForestClassifier (random_state = random_state )
420
- mapie_clf0 = MapieCalibrator (estimator = estimator , calibrator = calibrator )
421
- mapie_clf1 = MapieCalibrator (estimator = estimator , calibrator = calibrator )
422
- mapie_clf2 = MapieCalibrator (estimator = estimator , calibrator = calibrator )
409
+ mapie_clf0 = TopLabelCalibrator (estimator = estimator , calibrator = calibrator )
410
+ mapie_clf1 = TopLabelCalibrator (estimator = estimator , calibrator = calibrator )
411
+ mapie_clf2 = TopLabelCalibrator (estimator = estimator , calibrator = calibrator )
423
412
mapie_clf0 .fit (X , y , sample_weight = None , random_state = random_state )
424
413
mapie_clf1 .fit (
425
414
X , y , sample_weight = np .ones (shape = n_samples ),
@@ -463,7 +452,7 @@ def test_pipeline_compatibility() -> None:
463
452
)
464
453
pipe = make_pipeline (preprocessor , LogisticRegression ())
465
454
pipe .fit (X , y )
466
- mapie = MapieCalibrator (estimator = pipe )
455
+ mapie = TopLabelCalibrator (estimator = pipe )
467
456
mapie .fit (X , y )
468
457
mapie .predict (X )
469
458
@@ -476,7 +465,7 @@ def test_fit_parameters_passing() -> None:
476
465
"""
477
466
gb = GradientBoostingClassifier (random_state = random_state )
478
467
479
- mapie = MapieCalibrator (estimator = gb )
468
+ mapie = TopLabelCalibrator (estimator = gb )
480
469
481
470
def early_stopping_monitor (i , est , locals ):
482
471
"""Returns True on the 3rd iteration."""
0 commit comments