Passed
Push — master ( 588022...8a2a5a )
by Simon
01:36
created

hyperactive.opt.gfo._tree_structured_parzen_estimators   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 157
Duplicated Lines 97.45 %

Importance

Changes 0
Metric Value
wmc 3
eloc 56
dl 153
loc 157
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A TreeStructuredParzenEstimators.__init__() 31 31 1
A TreeStructuredParzenEstimators._get_gfo_class() 11 11 1
A TreeStructuredParzenEstimators.get_test_params() 26 26 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
from hyperactive.opt._adapters._gfo import _BaseGFOadapter
2
3
4 View Code Duplication
class TreeStructuredParzenEstimators(_BaseGFOadapter):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
    """Tree structured parzen estimators optimizer.
6
7
    Parameters
8
    ----------
9
    search_space : dict[str, list]
10
        The search space to explore. A dictionary with parameter
11
        names as keys and a numpy array as values.
12
        Optional, can be passed later via ``set_params``.
13
    initialize : dict[str, int], default={"grid": 4, "random": 2, "vertices": 4}
14
        The method to generate initial positions. A dictionary with
15
        the following key literals and the corresponding value type:
16
        {"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
17
    constraints : list[callable], default=[]
18
        A list of constraints, where each constraint is a callable.
19
        The callable returns `True` or `False` dependend on the input parameters.
20
    random_state : None, int, default=None
21
        If None, create a new random state. If int, create a new random state
22
        seeded with the value.
23
    rand_rest_p : float, default=0.1
24
        The probability of a random iteration during the the search process.
25
    warm_start_smbo
26
        The warm start for SMBO.
27
    max_sample_size : int
28
        The maximum number of points to sample.
29
    sampling : dict0
30
        The sampling method to use.
31
    replacement : bool
32
        Whether to sample with replacement.
33
    gamma_tpe : float
34
        The parameter for the Tree Structured Parzen Estimators
35
    n_iter : int, default=100
36
        The number of iterations to run the optimizer.
37
    verbose : bool, default=False
38
        If True, print the progress of the optimization process.
39
    experiment : BaseExperiment, optional
40
        The experiment to optimize parameters for.
41
        Optional, can be passed later via ``set_params``.
42
43
    Examples
44
    --------
45
    Basic usage of TreeStructuredParzenEstimators with a scikit-learn experiment:
46
47
    1. defining the experiment to optimize:
48
    >>> from hyperactive.experiment.integrations import SklearnCvExperiment
49
    >>> from sklearn.datasets import load_iris
50
    >>> from sklearn.svm import SVC
51
    >>>
52
    >>> X, y = load_iris(return_X_y=True)
53
    >>>
54
    >>> sklearn_exp = SklearnCvExperiment(
55
    ...     estimator=SVC(),
56
    ...     X=X,
57
    ...     y=y,
58
    ... )
59
60
    2. setting up the treeStructuredParzenEstimators optimizer:
61
    >>> from hyperactive.opt import TreeStructuredParzenEstimators
62
    >>> import numpy as np
63
    >>>
64
    >>> config = {
65
    ...     "search_space": {
66
    ...         "C": [0.01, 0.1, 1, 10],
67
    ...         "gamma": [0.0001, 0.01, 0.1, 1, 10],
68
    ...     },
69
    ...     "n_iter": 100,
70
    ... }
71
    >>> optimizer = TreeStructuredParzenEstimators(experiment=sklearn_exp, **config)
72
73
    3. running the optimization:
74
    >>> best_params = optimizer.run()
75
76
    Best parameters can also be accessed via:
77
    >>> best_params = optimizer.best_params_
78
    """
79
80
    _tags = {
81
        "info:name": "Tree Structured Parzen Estimators",
82
        "info:local_vs_global": "mixed",  # "local", "mixed", "global"
83
        "info:explore_vs_exploit": "mixed",  # "explore", "exploit", "mixed"
84
        "info:compute": "high",  # "low", "middle", "high"
85
    }
86
87
    def __init__(
88
        self,
89
        search_space=None,
90
        initialize=None,
91
        constraints=None,
92
        random_state=None,
93
        rand_rest_p=0.1,
94
        warm_start_smbo=None,
95
        max_sample_size=10000000,
96
        sampling=None,
97
        replacement=True,
98
        gamma_tpe=0.2,
99
        n_iter=100,
100
        verbose=False,
101
        experiment=None,
102
    ):
103
        self.random_state = random_state
104
        self.rand_rest_p = rand_rest_p
105
        self.warm_start_smbo = warm_start_smbo
106
        self.max_sample_size = max_sample_size
107
        self.sampling = sampling
108
        self.replacement = replacement
109
        self.gamma_tpe = gamma_tpe
110
        self.search_space = search_space
111
        self.initialize = initialize
112
        self.constraints = constraints
113
        self.n_iter = n_iter
114
        self.experiment = experiment
115
        self.verbose = verbose
116
117
        super().__init__()
118
119
    def _get_gfo_class(self):
120
        """Get the GFO class to use.
121
122
        Returns
123
        -------
124
        class
125
            The GFO class to use. One of the concrete GFO classes
126
        """
127
        from gradient_free_optimizers import TreeStructuredParzenEstimators
128
129
        return TreeStructuredParzenEstimators
130
131
    @classmethod
132
    def get_test_params(cls, parameter_set="default"):
133
        """Get the test parameters for the optimizer.
134
135
        Returns
136
        -------
137
        dict with str keys
138
            The test parameters dictionary.
139
        """
140
        import numpy as np
141
142
        params = super().get_test_params()
143
        experiment = params[0]["experiment"]
144
        more_params = {
145
            "experiment": experiment,
146
            "max_sample_size": 100,
147
            "replacement": True,
148
            "gamma_tpe": 0.01,
149
            "search_space": {
150
                "C": [0.01, 0.1, 1, 10],
151
                "gamma": [0.0001, 0.01, 0.1, 1, 10],
152
            },
153
            "n_iter": 100,
154
        }
155
        params.append(more_params)
156
        return params
157