Skip to content

Commit ec0dbf0

Browse files
MechCoderGaelVaroquaux
authored andcommitted
[MRG+1] Fixes predicting std and cov without fitting in GPR by default. (scikit-learn#9177)
* If self.kernel_ is None then self.kernel_ is set to RBFKernel in predict() as fit(). Fixes scikit-learn#6573 * xxx_ attributes can not be altered outside fit. Removing self.kernel_ from predict() and using previously implemented self.kernel attribute. * Cleanup and add non-regression-test
1 parent 22f0cf2 commit ec0dbf0

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

doc/whats_new.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ Bug fixes
273273
``max_iter`` if finds a large inlier group early. :issue:`8251` by :user:`aivision2020`.
274274

275275
- Fixed a bug where :class:`sklearn.naive_bayes.MultinomialNB` and :class:`sklearn.naive_bayes.BernoulliNB`
276-
failed when `alpha=0`. :issue:`5814` by :user:`Yichuan Liu <yl565>` and
276+
failed when `alpha=0`. :issue:`5814` by :user:`Yichuan Liu <yl565>` and
277277
:user:`Herilalaina Rakotoarison <herilalaina>`.
278278

279279
- Fixed a bug where :func:`datasets.make_moons` gives an
@@ -425,6 +425,11 @@ Bug fixes
425425
hence :func:`metrics.cohen_kappa_score`. :issue:`8354`, :issue:`7929`
426426
by `Joel Nothman`_ and :user:`Jon Crall <Erotemic>`.
427427

428+
- Fixed a bug in :class:`gaussian_process.GaussianProcessRegressor`
429+
when the standard deviation and covariance predicted without fit
430+
would fail with a unmeaningful error by default.
431+
:issue:`6573` by :user:`Quazi Marufur Rahman <qmaruf>` and
432+
`Manoj Kumar`_.
428433

429434
- Fixed the implementation of `explained_variance_`
430435
in :class:`decomposition.PCA`,

sklearn/gaussian_process/gpr.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,17 @@ def predict(self, X, return_std=False, return_cov=False):
297297
X = check_array(X)
298298

299299
if not hasattr(self, "X_train_"): # Unfitted;predict based on GP prior
300+
if self.kernel is None:
301+
kernel = (C(1.0, constant_value_bounds="fixed") *
302+
RBF(1.0, length_scale_bounds="fixed"))
303+
else:
304+
kernel = self.kernel
300305
y_mean = np.zeros(X.shape[0])
301306
if return_cov:
302-
y_cov = self.kernel(X)
307+
y_cov = kernel(X)
303308
return y_mean, y_cov
304309
elif return_std:
305-
y_var = self.kernel.diag(X)
310+
y_var = kernel.diag(X)
306311
return y_mean, np.sqrt(y_var)
307312
else:
308313
return y_mean

sklearn/gaussian_process/tests/test_gpr.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
from sklearn.utils.testing \
1616
import (assert_true, assert_greater, assert_array_less,
17-
assert_almost_equal, assert_equal, assert_raise_message)
17+
assert_almost_equal, assert_equal, assert_raise_message,
18+
assert_array_almost_equal)
1819

1920

2021
def f(x):
@@ -327,3 +328,19 @@ def test_duplicate_input():
327328

328329
assert_almost_equal(y_pred_equal, y_pred_similar)
329330
assert_almost_equal(y_std_equal, y_std_similar)
331+
332+
333+
def test_no_fit_default_predict():
334+
# Test that GPR predictions without fit does not break by default.
335+
default_kernel = (C(1.0, constant_value_bounds="fixed") *
336+
RBF(1.0, length_scale_bounds="fixed"))
337+
gpr1 = GaussianProcessRegressor()
338+
_, y_std1 = gpr1.predict(X, return_std=True)
339+
_, y_cov1 = gpr1.predict(X, return_cov=True)
340+
341+
gpr2 = GaussianProcessRegressor(kernel=default_kernel)
342+
_, y_std2 = gpr2.predict(X, return_std=True)
343+
_, y_cov2 = gpr2.predict(X, return_cov=True)
344+
345+
assert_array_almost_equal(y_std1, y_std2)
346+
assert_array_almost_equal(y_cov1, y_cov2)

0 commit comments

Comments
 (0)