Passed
Push — master ( c241e4...b050e9 )
by Simon
01:57
created

hyperactive.opt.optuna._gp_optimizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 121
Duplicated Lines 94.21 %

Importance

Changes 0
Metric Value
wmc 4
eloc 46
dl 114
loc 121
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GPOptimizer.__init__() 23 23 1
A GPOptimizer._get_optimizer() 19 19 2
A GPOptimizer.get_test_params() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""Gaussian Process optimizer."""
2
# copyright: hyperactive developers, MIT License (see LICENSE file)
3
4
from .._adapters._base_optuna_adapter import _BaseOptunaAdapter
5
6
7 View Code Duplication
class GPOptimizer(_BaseOptunaAdapter):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
    """Gaussian Process-based Bayesian optimizer.
9
10
    Parameters
11
    ----------
12
    param_space : dict[str, tuple or list or optuna distributions]
13
        The search space to explore. Dictionary with parameter names
14
        as keys and either tuples/lists of (low, high) or
15
        optuna distribution objects as values.
16
    n_trials : int, default=100
17
        Number of optimization trials.
18
    initialize : dict[str, int], default=None
19
        The method to generate initial positions. A dictionary with
20
        the following key literals and the corresponding value type:
21
        {"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
22
    random_state : None, int, default=None
23
        If None, create a new random state. If int, create a new random state
24
        seeded with the value.
25
    early_stopping : int, default=None
26
        Number of trials after which to stop if no improvement.
27
    max_score : float, default=None
28
        Maximum score threshold. Stop optimization when reached.
29
    n_startup_trials : int, default=10
30
        Number of startup trials for GP.
31
    deterministic_objective : bool, default=False
32
        Whether the objective function is deterministic.
33
    experiment : BaseExperiment, optional
34
        The experiment to optimize parameters for.
35
        Optional, can be passed later via ``set_params``.
36
37
    Examples
38
    --------
39
    Basic usage of GPOptimizer with a scikit-learn experiment:
40
41
    >>> from hyperactive.experiment.integrations import SklearnCvExperiment
42
    >>> from hyperactive.opt.optuna import GPOptimizer
43
    >>> from sklearn.datasets import load_iris
44
    >>> from sklearn.svm import SVC
45
    >>> X, y = load_iris(return_X_y=True)
46
    >>> sklearn_exp = SklearnCvExperiment(estimator=SVC(), X=X, y=y)
47
    >>> param_space = {
48
    ...     "C": (0.01, 10),
49
    ...     "gamma": (0.0001, 10),
50
    ... }
51
    >>> optimizer = GPOptimizer(
52
    ...     param_space=param_space, n_trials=50, experiment=sklearn_exp
53
    ... )
54
    >>> best_params = optimizer.run()
55
    """
56
57
    _tags = {
58
        "info:name": "Gaussian Process Optimizer",
59
        "info:local_vs_global": "global",
60
        "info:explore_vs_exploit": "exploit",
61
        "info:compute": "high",
62
        "python_dependencies": ["optuna"],
63
    }
64
65
    def __init__(
66
        self,
67
        param_space=None,
68
        n_trials=100,
69
        initialize=None,
70
        random_state=None,
71
        early_stopping=None,
72
        max_score=None,
73
        n_startup_trials=10,
74
        deterministic_objective=False,
75
        experiment=None,
76
    ):
77
        self.n_startup_trials = n_startup_trials
78
        self.deterministic_objective = deterministic_objective
79
80
        super().__init__(
81
            param_space=param_space,
82
            n_trials=n_trials,
83
            initialize=initialize,
84
            random_state=random_state,
85
            early_stopping=early_stopping,
86
            max_score=max_score,
87
            experiment=experiment,
88
        )
89
90
    def _get_optimizer(self):
91
        """Get the GP optimizer.
92
93
        Returns
94
        -------
95
        optimizer
96
            The Optuna GPOptimizer instance
97
        """
98
        import optuna
99
100
        optimizer_kwargs = {
101
            "n_startup_trials": self.n_startup_trials,
102
            "deterministic_objective": self.deterministic_objective,
103
        }
104
105
        if self.random_state is not None:
106
            optimizer_kwargs["seed"] = self.random_state
107
108
        return optuna.samplers.GPSampler(**optimizer_kwargs)
109
110
    @classmethod
111
    def get_test_params(cls, parameter_set="default"):
112
        """Return testing parameter settings for the optimizer."""
113
        params = super().get_test_params(parameter_set)
114
        params[0].update(
115
            {
116
                "n_startup_trials": 5,
117
                "deterministic_objective": True,
118
            }
119
        )
120
        return params
121