From baf8316ae46cd3f01239d5c16ede61261d6cc584 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 22 Mar 2024 13:03:46 +0000 Subject: [PATCH 1/5] Allow definition of random state at constructor --- mapie/regression/quantile_regression.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mapie/regression/quantile_regression.py b/mapie/regression/quantile_regression.py index 2635b0267..8f616718c 100644 --- a/mapie/regression/quantile_regression.py +++ b/mapie/regression/quantile_regression.py @@ -154,10 +154,12 @@ def __init__( method: str = "quantile", cv: Optional[str] = None, alpha: float = 0.1, + random_state: Optional[Union[int, np.random.RandomState]] = None, ) -> None: super().__init__( estimator=estimator, method=method, + random_state=random_state ) self.cv = cv self.alpha = alpha @@ -519,6 +521,7 @@ def fit( Controls the shuffling applied to the data before applying the split. Pass an int for reproducible output across multiple function calls. + Overrides random state used in constructor. See :term:`Glossary `. By default ``None``. @@ -571,7 +574,9 @@ def fit( checked_estimator = self._check_estimator(self.estimator) alpha = self._check_alpha(self.alpha) X, y = indexable(X, y) - random_state = check_random_state(random_state) + random_state = check_random_state( + random_state if random_state is not None else self.random_state + ) results = self._check_calib_set( X, y, From 38e50790920dbd7a8aef782aa149b7427740c780 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 22 Mar 2024 13:04:00 +0000 Subject: [PATCH 2/5] Add line to AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 5509241df..ccbd7686d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -40,4 +40,5 @@ Contributors * Pierre de Fréminville * Ambros Marzetta * Carl McBride Ellis +* Tiago Leon Melo To be continued ... From f58b2f1ee22a74535b99d9d2e94ad06cad8a4eaa Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 22 Mar 2024 13:14:47 +0000 Subject: [PATCH 3/5] Add small test --- mapie/tests/test_quantile_regression.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mapie/tests/test_quantile_regression.py b/mapie/tests/test_quantile_regression.py index 60a42ace5..4cbe59f1c 100644 --- a/mapie/tests/test_quantile_regression.py +++ b/mapie/tests/test_quantile_regression.py @@ -791,3 +791,17 @@ def early_stopping_monitor(i, est, locals): for estimator in mapie.estimators_: assert estimator.estimators_.shape[0] == 3 + +def test_random_state_is_set() -> None: + """ + Test that the same result is obtained if the random state + is provided through constructor or through fit() + """ + mapie = MapieQuantileRegressor() + mapie_rand = MapieQuantileRegressor(random_state=random_state) + mapie.fit(X, y, calib_size=0.5, random_state=random_state) + mapie_rand.fit(X, y, calib_size=0.5) + y_pred, y_pis = mapie.predict(X) + y_pred_rand, y_pis_rand = mapie_rand.predict(X) + np.testing.assert_allclose(y_pred, y_pred_rand, rtol=1e-2) + np.testing.assert_allclose(y_pis, y_pis_rand, rtol=1e-2) From b5e1b596dd90ac05c229b645e961b5ce40ebd7fc Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 22 Mar 2024 13:22:29 +0000 Subject: [PATCH 4/5] Update HISTORY.rst --- HISTORY.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.rst b/HISTORY.rst index 6319ee67f..a2ac2db36 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -19,6 +19,7 @@ History * Add possibility of passing fit parameters used by estimators. * Fix memory issue CQR when testing for upper and lower bounds. * Add Winkler Interval Score. +* Allow `random_state` to be passed when instancing a MapieQuantileRegressor 0.8.2 (2024-01-11) ------------------ From 15fa3d99a31bfc5fe5cc281b97f7e7ab80c3d010 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Fri, 22 Mar 2024 18:07:59 +0000 Subject: [PATCH 5/5] Clean up --- mapie/regression/quantile_regression.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mapie/regression/quantile_regression.py b/mapie/regression/quantile_regression.py index 8f616718c..cc96d47cb 100644 --- a/mapie/regression/quantile_regression.py +++ b/mapie/regression/quantile_regression.py @@ -574,9 +574,7 @@ def fit( checked_estimator = self._check_estimator(self.estimator) alpha = self._check_alpha(self.alpha) X, y = indexable(X, y) - random_state = check_random_state( - random_state if random_state is not None else self.random_state - ) + random_state = check_random_state(random_state or self.random_state) results = self._check_calib_set( X, y,