File: test_forest_opt.py

package info (click to toggle)
scikit-optimize 0.10.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,736 kB
  • sloc: python: 10,668; javascript: 438; makefile: 139; sh: 6
file content (81 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from functools import partial

import pytest
from sklearn.tree import DecisionTreeClassifier

from skopt import forest_minimize, gbrt_minimize
from skopt.benchmarks import bench1, bench2, bench3, bench4

MINIMIZERS = [
    ("ET", partial(forest_minimize, base_estimator='ET')),
    ("RF", partial(forest_minimize, base_estimator='RF')),
    ("gbrt", gbrt_minimize),
]


@pytest.mark.fast_test
@pytest.mark.parametrize("base_estimator", [42, DecisionTreeClassifier()])
def test_forest_minimize_api(base_estimator):
    # invalid string value
    with pytest.raises(ValueError):
        forest_minimize(lambda x: 0.0, [], base_estimator='abc')

    # not a string nor a regressor
    with pytest.raises(ValueError):
        forest_minimize(lambda x: 0.0, [], base_estimator=base_estimator)


def check_minimize(
    minimizer,
    func,
    y_opt,
    dimensions,
    margin,
    n_calls,
    n_initial_points=10,
    x0=None,
    n_jobs=1,
):
    for n in range(3):
        r = minimizer(
            func,
            dimensions,
            n_calls=n_calls,
            random_state=n,
            n_initial_points=n_initial_points,
            x0=x0,
            n_jobs=n_jobs,
        )
        assert r.fun < y_opt + margin


@pytest.mark.slow_test
@pytest.mark.parametrize("name, minimizer", MINIMIZERS)
def test_tree_based_minimize(name, minimizer):
    check_minimize(minimizer, bench1, 0.05, [(-2.0, 2.0)], 0.05, 25, 5)

    # XXX: We supply points at the edge of the search
    # space as an initial point to the minimizer.
    # This makes sure that the RF model can find the minimum even
    # if all the randomly sampled points are one side of the
    # the minimum, since for a decision tree any point greater than
    # max(sampled_points) would give a constant value.
    X0 = [[-5.6], [-5.8], [5.8], [5.6]]
    check_minimize(minimizer, bench2, -4.7, [(-6.0, 6.0)], 0.1, 20, 10, X0)
    check_minimize(minimizer, bench3, -0.4, [(-2.0, 2.0)], 0.05, 10, 5)
    check_minimize(minimizer, bench4, 1.0, [["-2", "-1", "0", "1", "2"]], 0.05, 5, 1)


@pytest.mark.slow_test
def test_tree_based_minimize_n_jobs():
    check_minimize(forest_minimize, bench1, 0.05, [(-2.0, 2.0)], 0.05, 25, 5, n_jobs=2)


@pytest.mark.fast_test
def test_categorical_integer():
    def f(params):
        return 0

    dims = [[1]]
    res = forest_minimize(f, dims, n_calls=1, random_state=1, n_initial_points=1)
    assert res.x_iters[0][0] == dims[0][0]