Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from scikit-optimize/master
Browse files Browse the repository at this point in the history
merge upstream
  • Loading branch information
holgern committed Jan 21, 2020
2 parents 2b46af3 + 8d91e04 commit 212bafc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
18 changes: 8 additions & 10 deletions skopt/learning/gaussian_process/tests/test_gpr.py
Expand Up @@ -3,11 +3,9 @@

from scipy import optimize

from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_false
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_array_equal
from numpy.testing import assert_almost_equal
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_array_equal

from skopt.learning import GaussianProcessRegressor
from skopt.learning.gaussian_process.kernels import RBF
Expand Down Expand Up @@ -40,12 +38,12 @@ def predict_wrapper(X, gpr):
def test_param_for_white_kernel_in_Sum(kernel):
kernel_with_noise = kernel + wk
wk_present, wk_param = _param_for_white_kernel_in_Sum(kernel + wk)
assert_true(wk_present)
assert wk_present
kernel_with_noise.set_params(
**{wk_param: WhiteKernel(noise_level=0.0)})
assert_array_equal(kernel_with_noise(X), kernel(X))

assert_false(_param_for_white_kernel_in_Sum(kernel5)[0])
assert not _param_for_white_kernel_in_Sum(kernel5)[0]


@pytest.mark.fast_test
Expand All @@ -54,13 +52,13 @@ def test_noise_equals_gaussian():

# gpr2 sets the noise component to zero at predict time.
gpr2 = GaussianProcessRegressor(rbf, noise="gaussian").fit(X, y)
assert_false(gpr1.noise_)
assert_true(gpr2.noise_)
assert not gpr1.noise_
assert gpr2.noise_
assert_almost_equal(gpr1.kernel_.k2.noise_level, gpr2.noise_, 4)
mean1, std1 = gpr1.predict(X, return_std=True)
mean2, std2 = gpr2.predict(X, return_std=True)
assert_array_almost_equal(mean1, mean2, 4)
assert_false(np.any(std1 == std2))
assert not np.any(std1 == std2)


@pytest.mark.fast_test
Expand Down
23 changes: 18 additions & 5 deletions skopt/learning/gaussian_process/tests/test_kernels.py
@@ -1,8 +1,13 @@
import numpy as np
from scipy import optimize
from scipy.spatial.distance import pdist, squareform
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_array_equal
try:
from sklearn.preprocessing import OrdinalEncoder
UseOrdinalEncoder = True
except ImportError:
UseOrdinalEncoder = False
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_array_equal
import pytest

from skopt.learning.gaussian_process import GaussianProcessRegressor
Expand Down Expand Up @@ -192,8 +197,16 @@ def test_gp_regressor():
["ham", "spam", "spam"]])
y = rng.randn(3)
hm = HammingKernel(length_scale=[1.0, 1.0, 1.0])
if UseOrdinalEncoder:
enc = OrdinalEncoder()
enc.fit(X)

gpr = GaussianProcessRegressor(hm)
gpr.fit(X, y)
assert_array_almost_equal(gpr.predict(X), y)
assert_array_almost_equal(gpr.predict(X[:2]), y[:2])
if UseOrdinalEncoder:
gpr.fit(enc.transform(X), y)
assert_array_almost_equal(gpr.predict(enc.transform(X)), y)
assert_array_almost_equal(gpr.predict(enc.transform(X[:2])), y[:2])
else:
gpr.fit(X, y)
assert_array_almost_equal(gpr.predict(X), y)
assert_array_almost_equal(gpr.predict(X[:2]), y[:2])
19 changes: 11 additions & 8 deletions skopt/learning/tests/test_gbrt.py
Expand Up @@ -6,10 +6,9 @@
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor

from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_almost_equal
from numpy.testing import assert_equal
from numpy.testing import assert_array_equal
from numpy.testing import assert_almost_equal

from skopt.learning import GradientBoostingQuantileRegressor

Expand Down Expand Up @@ -44,12 +43,15 @@ def test_gbrt_base_estimator():

base = RandomForestRegressor()
rgr = GradientBoostingQuantileRegressor(base_estimator=base)
assert_raise_message(ValueError, 'type GradientBoostingRegressor',
rgr.fit, X, y)
with pytest.raises(ValueError):
# 'type GradientBoostingRegressor',
rgr.fit(X, y)

base = GradientBoostingRegressor()
rgr = GradientBoostingQuantileRegressor(base_estimator=base)
assert_raise_message(ValueError, 'quantile loss', rgr.fit, X, y)
with pytest.raises(ValueError):
# 'quantile loss'
rgr.fit(X, y)

base = GradientBoostingRegressor(loss='quantile', n_estimators=20)
rgr = GradientBoostingQuantileRegressor(base_estimator=base)
Expand Down Expand Up @@ -82,7 +84,8 @@ def test_gbrt_with_std():
assert_array_equal(model.predict(X_).shape, (len(X_)))

l, c, h = model.predict(X_, return_quantiles=True).T
assert_equal(l.shape, c.shape, h.shape)
assert_equal(l.shape, c.shape)
assert_equal(c.shape, h.shape)
assert_equal(l.shape[0], X_.shape[0])

mean, std = model.predict(X_, return_std=True)
Expand Down
4 changes: 2 additions & 2 deletions skopt/tests/test_parallel_cl.py
Expand Up @@ -3,8 +3,8 @@
"""


from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_raises
from numpy.testing import assert_equal
from numpy.testing import assert_raises

from skopt.space import Real
from skopt import Optimizer
Expand Down

0 comments on commit 212bafc

Please sign in to comment.