From ec0dbf0399c4187a1d17e66de17265c67588373f Mon Sep 17 00:00:00 2001 From: Manoj Kumar <mks542@nyu.edu> Date: Mon, 26 Jun 2017 04:02:16 +0530 Subject: [PATCH] [MRG+1] Fixes predicting std and cov without fitting in GPR by default. (#9177) * If self.kernel_ is None then self.kernel_ is set to RBFKernel in predict() as fit(). Fixes #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 --- doc/whats_new.rst | 7 ++++++- sklearn/gaussian_process/gpr.py | 9 +++++++-- sklearn/gaussian_process/tests/test_gpr.py | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 04480f7879..761c98ef27 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -273,7 +273,7 @@ Bug fixes ``max_iter`` if finds a large inlier group early. :issue:`8251` by :user:`aivision2020`. - Fixed a bug where :class:`sklearn.naive_bayes.MultinomialNB` and :class:`sklearn.naive_bayes.BernoulliNB` - failed when `alpha=0`. :issue:`5814` by :user:`Yichuan Liu <yl565>` and + failed when `alpha=0`. :issue:`5814` by :user:`Yichuan Liu <yl565>` and :user:`Herilalaina Rakotoarison <herilalaina>`. - Fixed a bug where :func:`datasets.make_moons` gives an @@ -425,6 +425,11 @@ Bug fixes hence :func:`metrics.cohen_kappa_score`. :issue:`8354`, :issue:`7929` by `Joel Nothman`_ and :user:`Jon Crall <Erotemic>`. + - Fixed a bug in :class:`gaussian_process.GaussianProcessRegressor` + when the standard deviation and covariance predicted without fit + would fail with a unmeaningful error by default. + :issue:`6573` by :user:`Quazi Marufur Rahman <qmaruf>` and + `Manoj Kumar`_. - Fixed the implementation of `explained_variance_` in :class:`decomposition.PCA`, diff --git a/sklearn/gaussian_process/gpr.py b/sklearn/gaussian_process/gpr.py index 5c29c5258a..4f9ff9cee7 100644 --- a/sklearn/gaussian_process/gpr.py +++ b/sklearn/gaussian_process/gpr.py @@ -297,12 +297,17 @@ class GaussianProcessRegressor(BaseEstimator, RegressorMixin): X = check_array(X) if not hasattr(self, "X_train_"): # Unfitted;predict based on GP prior + if self.kernel is None: + kernel = (C(1.0, constant_value_bounds="fixed") * + RBF(1.0, length_scale_bounds="fixed")) + else: + kernel = self.kernel y_mean = np.zeros(X.shape[0]) if return_cov: - y_cov = self.kernel(X) + y_cov = kernel(X) return y_mean, y_cov elif return_std: - y_var = self.kernel.diag(X) + y_var = kernel.diag(X) return y_mean, np.sqrt(y_var) else: return y_mean diff --git a/sklearn/gaussian_process/tests/test_gpr.py b/sklearn/gaussian_process/tests/test_gpr.py index 1502a820f2..b645a6be18 100644 --- a/sklearn/gaussian_process/tests/test_gpr.py +++ b/sklearn/gaussian_process/tests/test_gpr.py @@ -14,7 +14,8 @@ from sklearn.gaussian_process.kernels import DotProduct from sklearn.utils.testing \ import (assert_true, assert_greater, assert_array_less, - assert_almost_equal, assert_equal, assert_raise_message) + assert_almost_equal, assert_equal, assert_raise_message, + assert_array_almost_equal) def f(x): @@ -327,3 +328,19 @@ def test_duplicate_input(): assert_almost_equal(y_pred_equal, y_pred_similar) assert_almost_equal(y_std_equal, y_std_similar) + + +def test_no_fit_default_predict(): + # Test that GPR predictions without fit does not break by default. + default_kernel = (C(1.0, constant_value_bounds="fixed") * + RBF(1.0, length_scale_bounds="fixed")) + gpr1 = GaussianProcessRegressor() + _, y_std1 = gpr1.predict(X, return_std=True) + _, y_cov1 = gpr1.predict(X, return_cov=True) + + gpr2 = GaussianProcessRegressor(kernel=default_kernel) + _, y_std2 = gpr2.predict(X, return_std=True) + _, y_cov2 = gpr2.predict(X, return_cov=True) + + assert_array_almost_equal(y_std1, y_std2) + assert_array_almost_equal(y_cov1, y_cov2) -- GitLab