Completed
Push — master ( 872a28...3bf564 )
by Simon
04:34 queued 11s
created

hyperactive.hyperactive.Hyperactive.predict()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
from .config import Config
7
from .opt_args import Arguments
8
from . import (
9
    HillClimbingOptimizer,
10
    StochasticHillClimbingOptimizer,
11
    TabuOptimizer,
12
    RandomSearchOptimizer,
13
    RandomRestartHillClimbingOptimizer,
14
    RandomAnnealingOptimizer,
15
    SimulatedAnnealingOptimizer,
16
    StochasticTunnelingOptimizer,
17
    ParallelTemperingOptimizer,
18
    ParticleSwarmOptimizer,
19
    EvolutionStrategyOptimizer,
20
    BayesianOptimizer,
21
)
22
23
24
class Hyperactive:
25
    def __init__(self, *args, **kwargs):
26
27
        """
28
29
        Parameters
30
        ----------
31
32
        search_config: dict
33
            A dictionary providing the model and hyperparameter search space for the
34
            optimization process.
35
        n_iter: int
36
            The number of iterations the optimizer performs.
37
        metric: string, optional (default: "accuracy")
38
            The metric the model is evaluated by.
39
        n_jobs: int, optional (default: 1)
40
            The number of searches to run in parallel.
41
        cv: int, optional (default: 3)
42
            The number of folds for the cross validation.
43
        verbosity: int, optional (default: 1)
44
            Verbosity level. 1 prints out warm_start points and their scores.
45
        random_state: int, optional (default: None)
46
            Sets the random seed.
47
        warm_start: dict, optional (default: False)
48
            Dictionary that definies a start point for the optimizer.
49
        memory: bool, optional (default: True)
50
            A memory, that saves the evaluation during the optimization to save time when
51
            optimizer returns to position.
52
        scatter_init: int, optional (default: False)
53
            Defines the number n of random positions that should be evaluated with 1/n the
54
            training data, to find a better initial position.
55
56
        Returns
57
        -------
58
        None
59
60
        """
61
62
        optimizer_dict = {
63
            "HillClimbing": HillClimbingOptimizer,
64
            "StochasticHillClimbing": StochasticHillClimbingOptimizer,
65
            "TabuSearch": TabuOptimizer,
66
            "RandomSearch": RandomSearchOptimizer,
67
            "RandomRestartHillClimbing": RandomRestartHillClimbingOptimizer,
68
            "RandomAnnealing": RandomAnnealingOptimizer,
69
            "SimulatedAnnealing": SimulatedAnnealingOptimizer,
70
            "StochasticTunneling": StochasticTunnelingOptimizer,
71
            "ParallelTempering": ParallelTemperingOptimizer,
72
            "ParticleSwarm": ParticleSwarmOptimizer,
73
            "EvolutionStrategy": EvolutionStrategyOptimizer,
74
            "Bayesian": BayesianOptimizer,
75
        }
76
77
        _config_ = Config(*args, **kwargs)
78
        _arg_ = Arguments(**kwargs)
79
80
        optimizer_class = optimizer_dict[_config_.optimizer]
81
        self._optimizer_ = optimizer_class(_config_, _arg_)
82
83
    def fit(self, X, y):
84
        """Public method for starting the search with the training data (X, y)
85
86
        Parameters
87
        ----------
88
        X : array-like or sparse matrix of shape = [n_samples, n_features]
89
90
        y : array-like, shape = [n_samples] or [n_samples, n_outputs]
91
92
        Returns
93
        -------
94
        None
95
        """
96
        self._optimizer_.fit(X, y)
97
98
    def predict(self, X_test):
99
        """Returns the prediction of X_test after a model was searched by `fit`
100
101
        Parameters
102
        ----------
103
        X_test : array-like or sparse matrix of shape = [n_samples, n_features]
104
105
        Returns
106
        -------
107
        (unnamed array) : array-like, shape = [n_samples] or [n_samples, n_outputs]
108
        """
109
        return self._optimizer_.predict(X_test)
110
111
    def score(self, X_test, y_true):
112
        """Returns the score calculated from the prediction of X_test and the true values from y_test
113
114
        Parameters
115
        ----------
116
        X_test : array-like or sparse matrix of shape = [n_samples, n_features]
117
118
        y_true : array-like, shape = [n_samples] or [n_samples, n_outputs]
119
120
        Returns
121
        -------
122
        (unnamed float) : float
123
        """
124
        return self._optimizer_.score(X_test, y_true)
125