Skip to content

API Gaussian Process: change default behavior of sample_y #25789

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

Open
wants to merge 1 commit into
base: main
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
7 changes: 7 additions & 0 deletions doc/whats_new/v1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ Changelog
- |Enhancement| Added the parameter `fill_value` to :class:`impute.IterativeImputer`.
:pr:`25232` by :user:`Thijs van Weezel <ValueInvestorThijs>`.

:mod:`sklearn.gaussian_process`
................................

- |API| The default value for `random_state` in
:meth:`GuassianProcessRegressor.sample_y` will change from `0` to `None` in v1.5.
:pr:`25789` by :user:`Kevin Ingles <ominusliticus>`.

:mod:`sklearn.inspection`
.........................

Expand Down
16 changes: 15 additions & 1 deletion sklearn/gaussian_process/_gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def predict(self, X, return_std=False, return_cov=False):
else:
return y_mean

def sample_y(self, X, n_samples=1, random_state=0):
def sample_y(self, X, n_samples=1, random_state="warn"):
"""Draw samples from Gaussian process and evaluate at X.

Parameters
Expand All @@ -476,6 +476,11 @@ def sample_y(self, X, n_samples=1, random_state=0):
Determines random number generation to randomly draw samples.
Pass an int for reproducible results across multiple function
calls.

.. versionchanged:: 1.5

The default value of `random_stae` will change to `None` in v1.5

See :term:`Glossary <random_state>`.

Returns
Expand All @@ -485,6 +490,15 @@ def sample_y(self, X, n_samples=1, random_state=0):
Values of n_samples samples drawn from Gaussian process and
evaluated at query points.
"""
# TODO(1.5): Default updates to `None`
if random_state == "warn":
warnings.warn(
"The default value of random_state will change from"
+ " 0 to None in 1.5",
Comment on lines +496 to +497
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should tell users how to silence the warning. Something like the following text:

Suggested change
"The default value of random_state will change from"
+ " 0 to None in 1.5",
"The default value of random_state will change from"
" 0 to None in version 1.5. Explicitly set a value for `random_state` "
"to silence this warning",

FutureWarning,
)
random_state = 0

rng = check_random_state(random_state)

y_mean, y_cov = self.predict(X, return_cov=True)
Expand Down
25 changes: 21 additions & 4 deletions sklearn/gaussian_process/tests/test_gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_sample_statistics(kernel):

y_mean, y_cov = gpr.predict(X2, return_cov=True)

samples = gpr.sample_y(X2, 300000)
samples = gpr.sample_y(X2, 300000, random_state=0)

# More digits accuracy would require many more samples
assert_almost_equal(y_mean, np.mean(samples, 1), 1)
Expand Down Expand Up @@ -368,8 +368,8 @@ def test_y_multioutput():
assert_almost_equal(y_std_1d, y_std_2d[..., target])
assert_almost_equal(y_cov_1d, y_cov_2d[..., target])

y_sample_1d = gpr.sample_y(X2, n_samples=10)
y_sample_2d = gpr_2d.sample_y(X2, n_samples=10)
y_sample_1d = gpr.sample_y(X2, n_samples=10, random_state=0)
y_sample_2d = gpr_2d.sample_y(X2, n_samples=10, random_state=0)

assert y_sample_1d.shape == (5, 10)
assert y_sample_2d.shape == (5, 2, 10)
Expand Down Expand Up @@ -769,10 +769,27 @@ def test_sample_y_shapes(normalize_y, n_targets):

model.fit(X_train, y_train)

y_samples = model.sample_y(X_test, n_samples=n_samples_y_test)
y_samples = model.sample_y(X_test, n_samples=n_samples_y_test, random_state=0)
assert y_samples.shape == y_test_shape


# TODO(1.5): Remove test when default changes
def test_sample_y_emit_warning():
"""Check that sample_y raises a warning if it uses the default parameter.

This is a test in association with issue:
https://github.com/scikit-learn/scikit-learn/issues/25767
"""
kernel = RBF(length_scale=1)
gpr = GaussianProcessRegressor(kernel=kernel)

warning_message = (
"The default value of random_state will change from 0 to None in 1.5"
)
with pytest.warns(FutureWarning, match=warning_message):
_ = gpr.sample_y(X)


class CustomKernel(C):
"""
A custom kernel that has a diag method that returns the first column of the
Expand Down