diff --git a/AUTHORS.rst b/AUTHORS.rst index a79a0da5b..461c1c815 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -40,5 +40,6 @@ Contributors * Pierre de Fréminville * Ambros Marzetta * Carl McBride Ellis +* Tiago Leon Melo * Baptiste Calot To be continued ... diff --git a/HISTORY.rst b/HISTORY.rst index cafd62cbb..d414b87fe 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -60,6 +60,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) ------------------ diff --git a/mapie/regression/quantile_regression.py b/mapie/regression/quantile_regression.py index e30646ab3..4a8259af5 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,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) + random_state = check_random_state(random_state or self.random_state) results = self._check_calib_set( X, y, 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)