Skip to content

Commit 8bffe03

Browse files
MTN: make MapieRegressor and MapieQuantileRegressor private
1 parent 3c36fff commit 8bffe03

19 files changed

+160
-199
lines changed

doc/api.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Regression
1313
mapie.regression.CrossConformalRegressor
1414
mapie.regression.JackknifeAfterBootstrapRegressor
1515
mapie.regression.ConformalizedQuantileRegressor
16-
mapie.regression.MapieRegressor
17-
mapie.regression.MapieQuantileRegressor
1816
mapie.regression.TimeSeriesRegressor
1917

2018
Classification

doc/v1_migration_guide.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ MAPIE v0.x Code
296296

297297
Below is a MAPIE v0.x code for split conformal prediction in case of pre-fitted model:
298298

299-
.. testcode::
299+
.. code:: python
300300
301301
from sklearn.linear_model import LinearRegression
302302
from mapie.regression import MapieRegressor
@@ -366,7 +366,7 @@ MAPIE v0.x code
366366

367367
Below is a MAPIE v0.x code for cross-conformal prediction:
368368

369-
.. testcode::
369+
.. code:: python
370370
371371
import numpy as np
372372
from sklearn.ensemble import RandomForestRegressor

examples/regression/1-quickstart/plot_prefit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def f(x: NDArray) -> NDArray:
180180
# three estimators need to be trained at quantile values of
181181
# ``(1+confidence_level)/2, (1-confidence_level)/2, 0.5)``.
182182

183-
# Train LGBMRegressor models for MapieQuantileRegressor
183+
# Train LGBMRegressor models for _MapieQuantileRegressor
184184
list_estimators_cqr = []
185185
for alpha_ in [(1 - confidence_level) / 2, (1 + confidence_level) / 2, 0.5]:
186186
estimator_ = LGBMRegressor(

examples/regression/1-quickstart/plot_ts-tutorial.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
the prediction intervals associated with one-step ahead forecasts through
3232
the EnbPI method.
3333
34-
As its parent class :class:`~MapieRegressor`,
3534
:class:`~mapie.regression.TimeSeriesRegressor` has two main arguments :
3635
"cv", and "method".
3736
In order to implement EnbPI, "method" must be set to "enbpi" (the default

examples/regression/2-advanced-analysis/plot_coverage_validity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def cumulative_average(arr):
300300
)
301301
empirical_coverages_ref.append(coverage)
302302

303-
# Compute empirical coverage for each trial with MapieRegressor
303+
# Compute empirical coverage for each trial
304304
conformalizer = SplitConformalRegressor(
305305
estimator=model, confidence_level=confidence_level, prefit=True
306306
)

mapie/quantile_regression.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

mapie/regression/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
from .quantile_regression import MapieQuantileRegressor, ConformalizedQuantileRegressor
1+
from .quantile_regression import ConformalizedQuantileRegressor
22
from .regression import (
3-
MapieRegressor,
43
SplitConformalRegressor,
54
CrossConformalRegressor,
65
JackknifeAfterBootstrapRegressor,
76
)
87
from .time_series_regression import TimeSeriesRegressor
98

109
__all__ = [
11-
"MapieRegressor",
12-
"MapieQuantileRegressor",
1310
"TimeSeriesRegressor",
1411
"SplitConformalRegressor",
1512
"CrossConformalRegressor",

mapie/regression/quantile_regression.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
check_estimator_fit_predict, check_lower_upper_bounds,
1919
check_null_weight, fit_estimator)
2020

21-
from .regression import MapieRegressor
21+
from .regression import _MapieRegressor
2222
from mapie_v1.utils import (
2323
cast_predictions_to_ndarray_tuple,
2424
prepare_params,
@@ -113,7 +113,7 @@ def __init__(
113113
self._is_fitted = prefit
114114
self._is_conformalized = False
115115

116-
self._mapie_quantile_regressor = MapieQuantileRegressor(
116+
self._mapie_quantile_regressor = _MapieQuantileRegressor(
117117
estimator=estimator,
118118
method="quantile",
119119
cv="prefit" if prefit else "split",
@@ -302,8 +302,12 @@ def predict(
302302
return predictions
303303

304304

305-
class MapieQuantileRegressor(MapieRegressor):
305+
class _MapieQuantileRegressor(_MapieRegressor):
306306
"""
307+
Note to users: _MapieQuantileRegressor is now private, and may change at any time.
308+
Please use ConformalizedQuantileRegressor instead.
309+
See the v1 migration guide for more information.
310+
307311
This class implements the conformalized quantile regression strategy
308312
as proposed by Romano et al. (2019) to make conformal predictions.
309313
@@ -372,12 +376,12 @@ class MapieQuantileRegressor(MapieRegressor):
372376
Examples
373377
--------
374378
>>> import numpy as np
375-
>>> from mapie.regression import MapieQuantileRegressor
379+
>>> from mapie.regression.quantile_regression import _MapieQuantileRegressor
376380
>>> X_train = np.array([[0], [1], [2], [3], [4], [5]])
377381
>>> y_train = np.array([5, 7.5, 9.5, 10.5, 12.5, 15])
378382
>>> X_calib = np.array([[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]])
379383
>>> y_calib = np.array([5, 7, 9, 4, 8, 1, 5, 7.5, 9.5, 12])
380-
>>> mapie_reg = MapieQuantileRegressor().fit(
384+
>>> mapie_reg = _MapieQuantileRegressor().fit(
381385
... X_train,
382386
... y_train,
383387
... X_calib=X_calib,
@@ -496,7 +500,7 @@ def _check_estimator(
496500
"""
497501
Perform several checks on the estimator to check if it has
498502
all the required specifications to be used with this methodology.
499-
The estimators that can be used in MapieQuantileRegressor need to
503+
The estimators that can be used in _MapieQuantileRegressor need to
500504
have a ``fit`` and ``predict`` attribute, but also need to allow
501505
a quantile loss and therefore also setting a quantile value.
502506
Note that there is a ``TypedDict`` to check which methods allow for
@@ -530,7 +534,7 @@ def _check_estimator(
530534
531535
ValueError
532536
There is no quantile ``"loss_name"`` and therefore this estimator
533-
can not be used as a ``MapieQuantileRegressor``.
537+
can not be used as a ``_MapieQuantileRegressor``.
534538
535539
ValueError
536540
The parameter to set the alpha value does not exist in this
@@ -714,7 +718,7 @@ def fit(
714718
shuffle: Optional[bool] = True,
715719
stratify: Optional[ArrayLike] = None,
716720
**fit_params,
717-
) -> MapieQuantileRegressor:
721+
) -> _MapieQuantileRegressor:
718722
"""
719723
Fit estimator and compute residuals used for prediction intervals.
720724
All the clones of the estimators for different quantile values are
@@ -786,7 +790,7 @@ def fit(
786790
787791
Returns
788792
-------
789-
MapieQuantileRegressor
793+
_MapieQuantileRegressor
790794
The model itself.
791795
"""
792796
self._initialize_fit_conformalize()
@@ -922,10 +926,10 @@ def conformalize(
922926
X: ArrayLike,
923927
y: ArrayLike,
924928
sample_weight: Optional[ArrayLike] = None,
925-
# Parameter groups kept for compliance with superclass MapieRegressor
929+
# Parameter groups kept for compliance with superclass _MapieRegressor
926930
groups: Optional[ArrayLike] = None,
927931
**kwargs: Any,
928-
) -> MapieRegressor:
932+
) -> _MapieRegressor:
929933
if self.cv == "prefit":
930934
self._initialize_and_check_prefit_estimators()
931935

mapie/regression/regression.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(
130130
# Note to developers: to implement this v1 class without touching the
131131
# v0 backend, we're for now using a hack. We always set cv="prefit",
132132
# and we fit the estimator if needed. See the .fit method below.
133-
self._mapie_regressor = MapieRegressor(
133+
self._mapie_regressor = _MapieRegressor(
134134
estimator=self._estimator,
135135
method="base",
136136
cv="prefit",
@@ -409,7 +409,7 @@ def __init__(
409409
)
410410
check_cv_not_string(cv)
411411

412-
self._mapie_regressor = MapieRegressor(
412+
self._mapie_regressor = _MapieRegressor(
413413
estimator=estimator,
414414
method=method,
415415
cv=cv,
@@ -722,7 +722,7 @@ def __init__(
722722

723723
cv = self._check_and_convert_resampling_to_cv(resampling)
724724

725-
self._mapie_regressor = MapieRegressor(
725+
self._mapie_regressor = _MapieRegressor(
726726
estimator=estimator,
727727
method=method,
728728
cv=cv,
@@ -909,8 +909,13 @@ def _check_and_convert_resampling_to_cv(
909909
return cv
910910

911911

912-
class MapieRegressor(RegressorMixin, BaseEstimator):
912+
class _MapieRegressor(RegressorMixin, BaseEstimator):
913913
"""
914+
Note to users: _MapieRegressor is now private, and may change at any time.
915+
Please use CrossConformalRegressor, CrossConformalRegressor or
916+
JackknifeAfterBootstrapRegressor instead.
917+
See the v1 migration guide for more information.
918+
914919
Prediction interval with out-of-fold conformity scores.
915920
916921
This class implements the jackknife+ strategy and its variations
@@ -1075,12 +1080,12 @@ class MapieRegressor(RegressorMixin, BaseEstimator):
10751080
Examples
10761081
--------
10771082
>>> import numpy as np
1078-
>>> from mapie.regression import MapieRegressor
1083+
>>> from mapie.regression.regression import _MapieRegressor
10791084
>>> from sklearn.linear_model import LinearRegression
10801085
>>> X_toy = np.array([[0], [1], [2], [3], [4], [5]])
10811086
>>> y_toy = np.array([5, 7.5, 9.5, 10.5, 12.5, 15])
10821087
>>> clf = LinearRegression().fit(X_toy, y_toy)
1083-
>>> mapie_reg = MapieRegressor(estimator=clf, cv="prefit")
1088+
>>> mapie_reg = _MapieRegressor(estimator=clf, cv="prefit")
10841089
>>> mapie_reg = mapie_reg.fit(X_toy, y_toy)
10851090
>>> y_pred, y_pis = mapie_reg.predict(X_toy, alpha=0.5)
10861091
>>> print(y_pis[:, :, 0])
@@ -1358,7 +1363,7 @@ def fit(
13581363
sample_weight: Optional[ArrayLike] = None,
13591364
groups: Optional[ArrayLike] = None,
13601365
**kwargs: Any
1361-
) -> MapieRegressor:
1366+
) -> _MapieRegressor:
13621367
"""
13631368
Fit estimator and compute conformity scores used for
13641369
prediction intervals.
@@ -1395,7 +1400,7 @@ def fit(
13951400
13961401
Returns
13971402
-------
1398-
MapieRegressor
1403+
_MapieRegressor
13991404
The model itself.
14001405
"""
14011406

@@ -1449,7 +1454,7 @@ def fit_estimator(
14491454
y: ArrayLike,
14501455
sample_weight: Optional[ArrayLike] = None,
14511456
groups: Optional[ArrayLike] = None,
1452-
) -> MapieRegressor:
1457+
) -> _MapieRegressor:
14531458

14541459
self.estimator_.fit_single_estimator(
14551460
X,
@@ -1468,7 +1473,7 @@ def conformalize(
14681473
sample_weight: Optional[ArrayLike] = None,
14691474
groups: Optional[ArrayLike] = None,
14701475
**kwargs: Any
1471-
) -> MapieRegressor:
1476+
) -> _MapieRegressor:
14721477

14731478
predict_params = kwargs.pop('predict_params', {})
14741479
self._predict_params = len(predict_params) > 0

mapie/regression/time_series_regression.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
from numpy.typing import ArrayLike, NDArray
1212
from mapie.conformity_scores import BaseRegressionScore
13-
from mapie.regression import MapieRegressor
13+
from mapie.regression.regression import _MapieRegressor
1414
from mapie.utils import check_alpha, check_gamma
1515
from mapie_v1.utils import transform_confidence_level_to_alpha_list
1616

1717

18-
class TimeSeriesRegressor(MapieRegressor):
18+
class TimeSeriesRegressor(_MapieRegressor):
1919
"""
2020
Prediction intervals with out-of-fold residuals for time series.
2121
This class only has two valid ``method`` : ``"enbpi"`` or ``"aci"``
@@ -54,8 +54,7 @@ class TimeSeriesRegressor(MapieRegressor):
5454
https://arxiv.org/pdf/2202.07282.pdf
5555
"""
5656

57-
cv_need_agg_function_ = MapieRegressor.cv_need_agg_function_ \
58-
+ ["BlockBootstrap"]
57+
cv_need_agg_function_ = _MapieRegressor.cv_need_agg_function_ + ["BlockBootstrap"]
5958
valid_methods_ = ["enbpi", "aci"]
6059
default_sym_ = False
6160

0 commit comments

Comments
 (0)