Skip to content

Commit 76b8edb

Browse files
committed
VTA comments taken into account
1 parent 5723c4a commit 76b8edb

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

examples/regression/1-quickstart/plot_prefit.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
In this example, we first fit a neural network on the training set. We
1313
then compute residuals on a validation set with the `cv="prefit"` parameter.
1414
Finally, we evaluate the model with prediction intervals on a testing set.
15-
We will also show how to use the prefit method in the comformalized quantile
15+
We will also show how to use the prefit method in the conformalized quantile
1616
regressor.
1717
"""
1818

@@ -23,12 +23,13 @@
2323
import scipy
2424
from sklearn.model_selection import train_test_split
2525
from sklearn.neural_network import MLPRegressor
26+
import warnings
2627

2728
from mapie.regression import MapieRegressor
2829
from mapie.quantile_regression import MapieQuantileRegressor
2930
from mapie.metrics import regression_coverage_score
3031
from mapie._typing import NDArray
31-
import warnings
32+
3233
warnings.filterwarnings("ignore")
3334

3435
alpha = 0.1
@@ -38,8 +39,8 @@
3839
# -----------------------------------------------------------------------------
3940
#
4041
# We start by defining a function that we will use to generate data. We then
41-
# add random noise y values. Then we split the dataset to have a training,
42-
# calibration and test set.
42+
# add random noise dependent variable values. Then we split the dataset to have
43+
# a training, calibration and test set.
4344

4445

4546
def f(x: NDArray) -> NDArray:
@@ -67,12 +68,12 @@ def f(x: NDArray) -> NDArray:
6768
# -----------------------------------------------------------------------------
6869
#
6970
# For this example, we will train a MLPRegressor for
70-
# :class:`mapie.regression.MapieRegressor` and multiple LGBMRegressor in the
71-
# with a quantile objective as this is a requirement to perform conformalized
71+
# :class:`mapie.regression.MapieRegressor` and multiple LGBMRegressor with a
72+
# quantile objective as this is a requirement to perform conformalized
7273
# quantile regression using
7374
# :class:`mapie.quanitle_regression.MapieQuantileRegressor`. Note that the
7475
# three estimators need to be trained at quantile values of
75-
# $(\alpha/2, 1-(\alpha/2), 0.5)$.
76+
# :math:`(\alpha/2, 1-(\alpha/2), 0.5)`.
7677

7778

7879
# Train a MLPRegressor for MapieRegressor
@@ -81,7 +82,7 @@ def f(x: NDArray) -> NDArray:
8182

8283
# Train LGBMRegressor models for MapieQuantileRegressor
8384
list_estimators_cqr = []
84-
for alpha_ in [alpha/2, (1-(alpha/2)), 0.5]:
85+
for alpha_ in [alpha / 2, (1 - (alpha / 2)), 0.5]:
8586
estimator_ = LGBMRegressor(
8687
objective='quantile',
8788
alpha=alpha_,
@@ -94,8 +95,8 @@ def f(x: NDArray) -> NDArray:
9495
# 3. Using MAPIE to calibrate the models
9596
# -----------------------------------------------------------------------------
9697
#
97-
# We will now proceed to calibrate the models using MAPIE. This means using
98-
# the `cv="prefit"` so that we use the models that we already trained prior.
98+
# We will now proceed to calibrate the models using MAPIE. To this aim, we set
99+
# `cv="prefit"` so that we use the models that we already trained prior.
99100
# We then precict using the test set and evaluate its coverage.
100101

101102

@@ -124,7 +125,7 @@ def f(x: NDArray) -> NDArray:
124125
# 4. Plots
125126
# -----------------------------------------------------------------------------
126127
#
127-
# In order to view the results shown above, we will plot each othe predictions
128+
# In order to view the results shown above, we will plot each other predictions
128129
# with their prediction interval. The multi-layer perceptron (MLP) with
129130
# :class:`mapie.regression.MapieRegressor` and LGBMRegressor with
130131
# :class:`mapie.quantile_regression.MapieQuantileRegressor`.

mapie/tests/test_quantile_regression.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ def test_linear_regression_results(strategy: str) -> None:
519519
np.testing.assert_allclose(coverage, COVERAGES[strategy], rtol=1e-2)
520520

521521

522-
@pytest.mark.parametrize("estimator", [-1, 3, KFold(), LeaveOneOut()])
522+
@pytest.mark.parametrize("estimator", [-1, 3, 0.2])
523523
def test_quantile_prefit_non_list(estimator: Any) -> None:
524524
"""
525525
Test that there is a list of estimators provided when cv='prefit'
@@ -633,12 +633,14 @@ def test_warning_alpha_prefit() -> None:
633633
)
634634

635635

636-
def test_prefit_and_non_prefit_equal() -> None:
636+
@pytest.mark.parametrize("alpha", [0.05, 0.1, 0.2, 0.3])
637+
def test_prefit_and_non_prefit_equal(alpha: float) -> None:
637638
"""
638-
Check that the user is warned that the alphas need to be correctly set.
639+
Check that when using prefit and not prefit, the same values
640+
are found.
639641
"""
640642
list_estimators = []
641-
alphas_ = [0.15, 0.85, 0.5]
643+
alphas_ = [alpha/2, 1-(alpha/2), 0.5]
642644
for alpha_ in alphas_:
643645
est = clone(qt)
644646
params = {"quantile": alpha_}
@@ -648,14 +650,14 @@ def test_prefit_and_non_prefit_equal() -> None:
648650
mapie_reg_prefit = MapieQuantileRegressor(
649651
estimator=list_estimators,
650652
cv="prefit",
651-
alpha=0.3
653+
alpha=alpha
652654
)
653655
mapie_reg_prefit.fit(X_calib, y_calib)
654656
y_pred_prefit, y_pis_prefit = mapie_reg_prefit.predict(X)
655657

656658
mapie_reg = MapieQuantileRegressor(
657659
estimator=qt,
658-
alpha=0.3
660+
alpha=alpha
659661
)
660662
mapie_reg.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib)
661663
y_pred, y_pis = mapie_reg.predict(X)
@@ -664,6 +666,52 @@ def test_prefit_and_non_prefit_equal() -> None:
664666
np.testing.assert_allclose(y_pis_prefit, y_pis)
665667

666668

669+
@pytest.mark.parametrize("alpha", [0.05, 0.1, 0.2, 0.3])
670+
def test_prefit_different_type_list_tuple_array(alpha: float) -> None:
671+
"""
672+
Check that as long as the estimators are given in a Iterable object,
673+
the we have the same results for each.}
674+
"""
675+
list_estimators = []
676+
alphas_ = [alpha/2, 1-(alpha/2), 0.5]
677+
for alpha_ in alphas_:
678+
est = clone(qt)
679+
params = {"quantile": alpha_}
680+
est.set_params(**params)
681+
est.fit(X_train, y_train)
682+
list_estimators.append(est)
683+
684+
mapie_reg_prefit_list = MapieQuantileRegressor(
685+
estimator=list_estimators,
686+
cv="prefit",
687+
alpha=alpha
688+
)
689+
mapie_reg_prefit_list.fit(X_calib, y_calib)
690+
y_pred_prefit_list, y_pis_prefit_list = mapie_reg_prefit_list.predict(X)
691+
692+
mapie_reg_prefit_tuple = MapieQuantileRegressor(
693+
estimator=tuple(list_estimators),
694+
cv="prefit",
695+
alpha=alpha
696+
)
697+
mapie_reg_prefit_tuple.fit(X_calib, y_calib)
698+
y_pred_prefit_tuple, y_pis_prefit_tuple = mapie_reg_prefit_tuple.predict(X)
699+
700+
mapie_reg_prefit_array = MapieQuantileRegressor(
701+
estimator=np.array(list_estimators),
702+
cv="prefit",
703+
alpha=alpha
704+
)
705+
mapie_reg_prefit_array.fit(X_calib, y_calib)
706+
y_pred_prefit_array, y_pis_prefit_array = mapie_reg_prefit_array.predict(X)
707+
708+
np.testing.assert_allclose(y_pred_prefit_list, y_pred_prefit_tuple)
709+
np.testing.assert_allclose(y_pis_prefit_list, y_pis_prefit_tuple)
710+
711+
np.testing.assert_allclose(y_pred_prefit_list, y_pred_prefit_array)
712+
np.testing.assert_allclose(y_pis_prefit_list, y_pis_prefit_array)
713+
714+
667715
@pytest.mark.parametrize("estimator", ESTIMATOR)
668716
def test_pipeline_compatibility(estimator: RegressorMixin) -> None:
669717
"""Check that MAPIE works on pipeline based on pandas dataframes"""

mapie/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_alpha_in_predict() -> None:
242242
mapie_reg.predict(X, ensemble=True)
243243

244244

245-
@pytest.mark.parametrize("estimator", [-1, 3])
245+
@pytest.mark.parametrize("estimator", [-1, 3, 0.2])
246246
def test_quantile_prefit_non_iterable(estimator: Any) -> None:
247247
"""
248248
Test that there is a list of estimators provided when cv='prefit'

0 commit comments

Comments
 (0)