Passed
Push — master ( 9587b2...72341c )
by Simon
01:53
created

tests.test_arguments_api.test_load_memory()   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 16
rs 9.75
c 0
b 0
f 0
cc 1
nop 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
import pandas as pd
7
8
from sklearn.datasets import load_iris
9
from sklearn.model_selection import cross_val_score
10
from sklearn.tree import DecisionTreeClassifier
11
from hyperactive import Hyperactive
12
13
data = load_iris()
14
X = data.data
15
y = data.target
16
17
18
def model(para, X, y):
19
    model = DecisionTreeClassifier(
20
        max_depth=para["max_depth"],
21
        min_samples_split=para["min_samples_split"],
22
        min_samples_leaf=para["min_samples_leaf"],
23
    )
24
    scores = cross_val_score(model, X, y, cv=3)
25
26
    return scores.mean()
27
28
29
search_config = {
30
    model: {
31
        "max_depth": range(1, 21),
32
        "min_samples_split": range(2, 21),
33
        "min_samples_leaf": range(1, 21),
34
    }
35
}
36
37
warm_start = {model: {"max_depth": 2, "min_samples_split": 2, "min_samples_leaf": 2}}
38
39
40
def test_func_return():
41
    def model1(para, X, y):
42
        model = DecisionTreeClassifier(
43
            criterion=para["criterion"],
44
            max_depth=para["max_depth"],
45
            min_samples_split=para["min_samples_split"],
46
            min_samples_leaf=para["min_samples_leaf"],
47
        )
48
        scores = cross_val_score(model, X, y, cv=3)
49
50
        return scores.mean(), model
51
52
    search_config1 = {
53
        model1: {
54
            "criterion": ["gini", "entropy"],
55
            "max_depth": range(1, 21),
56
            "min_samples_split": range(2, 21),
57
            "min_samples_leaf": range(1, 21),
58
        }
59
    }
60
61
    opt = Hyperactive(search_config1)
62
    opt.search(X, y)
63
64
65
def test_n_jobs_2():
66
    opt = Hyperactive(search_config, n_jobs=2)
67
    opt.search(X, y)
68
69
70
def test_n_jobs_4():
71
    opt = Hyperactive(search_config, n_jobs=4)
72
    opt.search(X, y)
73
74
75
def test_positional_args():
76
    opt0 = Hyperactive(search_config, random_state=False)
77
    opt0.search(X, y)
78
79
    opt1 = Hyperactive(search_config, random_state=1)
80
    opt1.search(X, y)
81
82
    opt2 = Hyperactive(search_config, random_state=1)
83
    opt2.search(X, y)
84
85
86
def test_random_state():
87
    opt0 = Hyperactive(search_config, random_state=False)
88
    opt0.search(X, y)
89
90
    opt1 = Hyperactive(search_config, random_state=0)
91
    opt1.search(X, y)
92
93
    opt2 = Hyperactive(search_config, random_state=1)
94
    opt2.search(X, y)
95
96
97
def test_max_time():
98
    opt0 = Hyperactive(search_config, max_time=0.001)
99
    opt0.search(X, y)
100
101
102
def test_memory():
103
    opt0 = Hyperactive(search_config, memory=True)
104
    opt0.search(X, y)
105
106
    opt1 = Hyperactive(search_config, memory=False)
107
    opt1.search(X, y)
108
109
110
def test_verbosity():
111
    opt0 = Hyperactive(search_config, verbosity=0)
112
    opt0.search(X, y)
113
114
    opt0 = Hyperactive(search_config, n_jobs=2, verbosity=0)
115
    opt0.search(X, y)
116
117
    opt1 = Hyperactive(search_config, verbosity=1)
118
    opt1.search(X, y)
119
120
    opt0 = Hyperactive(search_config, n_jobs=2, verbosity=1)
121
    opt0.search(X, y)
122
123
    opt1 = Hyperactive(search_config, verbosity=2)
124
    opt1.search(X, y)
125
126
127
def test_scatter_init():
128
    opt = Hyperactive(search_config, scatter_init=10)
129
    opt.search(X, y)
130
131
132
def test_optimizer_args():
133
    opt = Hyperactive(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}})
134
    opt.search(X, y)
135
136
137
def test_scatter_init_and_warm_start():
138
    opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10)
139
    opt.search(X, y)
140
141
    opt = Hyperactive(search_config, warm_start=warm_start, scatter_init=10)
142
    opt.search(X, y)
143
144
145
def test_warm_start_multiple_jobs():
146
    opt = Hyperactive(search_config, n_jobs=4, warm_start=warm_start)
147
    opt.search(X, y)
148
149
150
def test_warm_start():
151
    opt = Hyperactive(search_config, n_jobs=1, warm_start=warm_start)
152
    opt.search(X, y)
153
154
155
def test_get_search_path():
156
    opt = Hyperactive(search_config, get_search_path=True)
157
    opt.search(X, y)
158
159
    opt = Hyperactive(search_config, optimizer="ParticleSwarm", get_search_path=True)
160
    opt.search(X, y)
161
162
163
def test_load_memory():
164
    para = pd.DataFrame(
165
        np.array([[2, 2, 2, 2, 2], [2, 2, 2, 2, 2]]),
166
        columns=[
167
            "N_columns",
168
            "N_rows",
169
            "max_depth",
170
            "min_samples_leaf",
171
            "min_samples_split",
172
        ],
173
    )
174
    score = pd.DataFrame(np.array([1, 1]), columns=["mean_test_score"])
175
176
    opt = Hyperactive(search_config, n_iter=3, meta_learn=True)
177
    opt.search(X, y)
178
    opt._optimizer_.search(0, X, y)._space_.load_memory(para, score)
179
180
181
test_load_memory()
182