Skip to content

Commit fcc75a0

Browse files
authored
Merge branch 'master' into 503-404-citations-file-in-readthedocs
2 parents 1c9658b + 6ebf607 commit fcc75a0

File tree

7 files changed

+241
-49
lines changed

7 files changed

+241
-49
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ History
66
------------------
77

88
* Fix citations and license links
9+
* Fix the CQR tutorial to have same data in both methods
10+
* Add `** predict_params` in fit and predict method for Mapie Classifier
911
* Add Mondrian Conformal Prediction for regression and classification
1012
* Add `** predict_params` in fit and predict method for Mapie Regression
1113
* Update the ts-changepoint notebook with the tutorial

examples/regression/4-tutorials/plot_cqr_tutorial.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ class :class:`~mapie.subsample.Subsample` (note that the `alpha` parameter is
101101
y['MedHouseVal'],
102102
random_state=random_state
103103
)
104-
X_train, X_calib, y_train, y_calib = train_test_split(
105-
X_train,
106-
y_train,
107-
random_state=random_state
108-
)
109104

110105

111106
##############################################################################
@@ -267,13 +262,19 @@ def plot_prediction_intervals(
267262
if strategy == "cqr":
268263
mapie = MapieQuantileRegressor(estimator, **params)
269264
mapie.fit(
270-
X_train, y_train,
271-
X_calib=X_calib, y_calib=y_calib,
265+
X_train,
266+
y_train,
267+
calib_size=0.3,
272268
random_state=random_state
273269
)
274270
y_pred[strategy], y_pis[strategy] = mapie.predict(X_test)
275271
else:
276-
mapie = MapieRegressor(estimator, **params, random_state=random_state)
272+
mapie = MapieRegressor(
273+
estimator,
274+
test_size=0.3,
275+
random_state=random_state,
276+
**params
277+
)
277278
mapie.fit(X_train, y_train)
278279
y_pred[strategy], y_pis[strategy] = mapie.predict(X_test, alpha=0.2)
279280
(

mapie/classification.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import warnings
4-
from typing import Iterable, Optional, Tuple, Union, cast
4+
from typing import Any, Iterable, Optional, Tuple, Union, cast
55

66
import numpy as np
77
from sklearn.base import BaseEstimator, ClassifierMixin
@@ -20,7 +20,8 @@
2020
from mapie.estimator.classifier import EnsembleClassifier
2121
from mapie.utils import (check_alpha, check_alpha_and_n_samples, check_cv,
2222
check_estimator_classification, check_n_features_in,
23-
check_n_jobs, check_null_weight, check_verbose)
23+
check_n_jobs, check_null_weight, check_predict_params,
24+
check_verbose)
2425

2526

2627
class MapieClassifier(BaseEstimator, ClassifierMixin):
@@ -419,7 +420,7 @@ def fit(
419420
sample_weight: Optional[ArrayLike] = None,
420421
size_raps: Optional[float] = None,
421422
groups: Optional[ArrayLike] = None,
422-
**fit_params,
423+
**kwargs: Any
423424
) -> MapieClassifier:
424425
"""
425426
Fit the base estimator or use the fitted base estimator.
@@ -453,14 +454,22 @@ def fit(
453454
454455
By default ``None``.
455456
456-
**fit_params : dict
457-
Additional fit parameters.
457+
kwargs : dict
458+
Additional fit and predict parameters.
458459
459460
Returns
460461
-------
461462
MapieClassifier
462463
The model itself.
463464
"""
465+
fit_params = kwargs.pop('fit_params', {})
466+
predict_params = kwargs.pop('predict_params', {})
467+
468+
if len(predict_params) > 0:
469+
self._predict_params = True
470+
else:
471+
self._predict_params = False
472+
464473
# Checks
465474
(estimator,
466475
self.conformity_score_function_,
@@ -496,7 +505,7 @@ def fit(
496505

497506
# Predict on calibration data
498507
y_pred_proba, y, y_enc = self.estimator_.predict_proba_calib(
499-
X, y, y_enc, groups
508+
X, y, y_enc, groups, **predict_params
500509
)
501510

502511
# Compute the conformity scores
@@ -506,15 +515,15 @@ def fit(
506515
y, y_pred_proba, y_enc=y_enc, X=X,
507516
sample_weight=sample_weight, groups=groups
508517
)
509-
510518
return self
511519

512520
def predict(
513521
self,
514522
X: ArrayLike,
515523
alpha: Optional[Union[float, Iterable[float]]] = None,
516524
include_last_label: Optional[Union[bool, str]] = True,
517-
agg_scores: Optional[str] = "mean"
525+
agg_scores: Optional[str] = "mean",
526+
**predict_params
518527
) -> Union[NDArray, Tuple[NDArray, NDArray]]:
519528
"""
520529
Prediction and prediction sets on new samples based on target
@@ -571,6 +580,9 @@ def predict(
571580
572581
By default "mean".
573582
583+
predict_params : dict
584+
Additional predict parameters.
585+
574586
Returns
575587
-------
576588
Union[NDArray, Tuple[NDArray, NDArray]]
@@ -581,11 +593,16 @@ def predict(
581593
(n_samples,) and (n_samples, n_classes, n_alpha) if alpha is not None.
582594
"""
583595
# Checks
596+
597+
if hasattr(self, '_predict_params'):
598+
check_predict_params(self._predict_params,
599+
predict_params, self.cv)
600+
584601
check_is_fitted(self, self.fit_attributes)
585602
alpha = cast(Optional[NDArray], check_alpha(alpha))
586603

587604
# Estimate predictions
588-
y_pred = self.estimator_.single_estimator_.predict(X)
605+
y_pred = self.estimator_.single_estimator_.predict(X, **predict_params)
589606
if alpha is None:
590607
return y_pred
591608

mapie/estimator/classifier.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def _predict_proba_oof_estimator(
222222
self,
223223
estimator: ClassifierMixin,
224224
X: ArrayLike,
225+
**predict_params
225226
) -> NDArray:
226227
"""
227228
Predict probabilities of a test set from a fitted estimator.
@@ -239,7 +240,7 @@ def _predict_proba_oof_estimator(
239240
ArrayLike
240241
Predicted probabilities.
241242
"""
242-
y_pred_proba = estimator.predict_proba(X)
243+
y_pred_proba = estimator.predict_proba(X, **predict_params)
243244
# we enforce y_pred_proba to contain all labels included in y
244245
if len(estimator.classes_) != self.n_classes:
245246
y_pred_proba = fix_number_of_classes(
@@ -252,7 +253,8 @@ def _predict_proba_calib_oof_estimator(
252253
estimator: ClassifierMixin,
253254
X: ArrayLike,
254255
val_index: ArrayLike,
255-
k: int
256+
k: int,
257+
**predict_params
256258
) -> Tuple[NDArray, ArrayLike, ArrayLike]:
257259
"""
258260
Perform predictions on a single out-of-fold model on a validation set.
@@ -276,7 +278,8 @@ def _predict_proba_calib_oof_estimator(
276278

277279
X_val = _safe_indexing(X, val_index)
278280
if _num_samples(X_val) > 0:
279-
y_pred_proba = self._predict_proba_oof_estimator(estimator, X_val)
281+
y_pred_proba = self._predict_proba_oof_estimator(estimator, X_val,
282+
**predict_params)
280283
else:
281284
y_pred_proba = np.array([])
282285
val_id = np.full(len(X_val), k, dtype=int)
@@ -401,6 +404,9 @@ def predict_proba_calib(
401404
402405
By default ``None``.
403406
407+
**predict_params : dict
408+
Additional predict parameters.
409+
404410
Returns
405411
-------
406412
NDArray of shape (n_samples_test, 1)
@@ -417,7 +423,7 @@ def predict_proba_calib(
417423
cv = cast(BaseCrossValidator, self.cv)
418424
outputs = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
419425
delayed(self._predict_proba_calib_oof_estimator)(
420-
estimator, X, calib_index, k
426+
estimator, X, calib_index, k, **predict_params
421427
)
422428
for k, ((_, calib_index), estimator) in enumerate(
423429
zip(cv.split(X, y, groups), self.estimators_)
@@ -462,6 +468,9 @@ def predict(
462468
How to aggregate the scores output by the estimators on test data
463469
if a cross-validation strategy is used
464470
471+
**predict_params : dict
472+
Additional predict parameters.
473+
465474
Returns
466475
-------
467476
NDArray
@@ -471,14 +480,15 @@ def predict(
471480
check_is_fitted(self, self.fit_attributes)
472481

473482
if self.cv == "prefit":
474-
y_pred_proba = self.single_estimator_.predict_proba(X)
483+
y_pred_proba = self.single_estimator_.predict_proba(
484+
X, **predict_params
485+
)
475486
else:
476487
y_pred_proba_k = np.asarray(
477-
Parallel(
478-
n_jobs=self.n_jobs, verbose=self.verbose
479-
)(
480-
delayed(self._predict_proba_oof_estimator)(estimator, X)
481-
for estimator in self.estimators_
488+
Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
489+
delayed(self._predict_proba_oof_estimator)(
490+
estimator, X, **predict_params
491+
) for estimator in self.estimators_
482492
)
483493
)
484494
if agg_scores == "crossval":

0 commit comments

Comments
 (0)