Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random state to MapieQuantileRegressor constructor #424

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ Contributors
* Pierre de Fréminville <pidefrem>
* Ambros Marzetta <ambrosm>
* Carl McBride Ellis <Carl-McBride-Ellis>
* Tiago Leon Melo <[email protected]>
* Baptiste Calot <[email protected]>
To be continued ...
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
5 changes: 4 additions & 1 deletion mapie/regression/quantile_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <random_state>`.

By default ``None``.
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions mapie/tests/test_quantile_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading