Passed
Push — master ( 3f2c9d...310ec2 )
by Simon
01:51
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(X, y)
62
    opt.search(search_config1)
63
64
65
def test_n_jobs_2():
66
    opt = Hyperactive(X, y)
67
    opt.search(search_config, n_jobs=2)
68
69
70
def test_n_jobs_4():
71
    opt = Hyperactive(X, y)
72
    opt.search(search_config, n_jobs=4)
73
74
75
def test_positional_args():
76
    opt0 = Hyperactive(X, y, random_state=False)
77
    opt0.search(search_config)
78
79
    opt1 = Hyperactive(X, y, random_state=1)
80
    opt1.search(search_config)
81
82
    opt2 = Hyperactive(X, y, random_state=1)
83
    opt2.search(search_config)
84
85
86
def test_random_state():
87
    opt0 = Hyperactive(X, y, random_state=False)
88
    opt0.search(search_config)
89
90
    opt1 = Hyperactive(X, y, random_state=0)
91
    opt1.search(search_config)
92
93
    opt2 = Hyperactive(X, y, random_state=1)
94
    opt2.search(search_config)
95
96
97
def test_max_time():
98
    opt0 = Hyperactive(X, y)
99
    opt0.search(search_config, max_time=0.001)
100
101
102
def test_memory():
103
    opt0 = Hyperactive(X, y, memory=True)
104
    opt0.search(search_config)
105
106
    opt1 = Hyperactive(X, y, memory=False)
107
    opt1.search(search_config)
108
109
110
def test_verbosity():
111
    opt0 = Hyperactive(X, y, verbosity=0)
112
    opt0.search(search_config)
113
114
    opt0 = Hyperactive(X, y, verbosity=0)
115
    opt0.search(search_config, n_jobs=2)
116
117
    opt1 = Hyperactive(X, y, verbosity=1)
118
    opt1.search(search_config)
119
120
    opt0 = Hyperactive(X, y, verbosity=1)
121
    opt0.search(search_config)
122
123
    opt1 = Hyperactive(X, y, verbosity=2)
124
    opt1.search(search_config, n_jobs=2)
125
126
127
def test_scatter_init():
128
    opt = Hyperactive(X, y)
129
    opt.search(search_config, scatter_init=10)
130
131
132
def test_optimizer_args():
133
    opt = Hyperactive(X, y)
134
    opt.search(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}})
135
136
137
def test_scatter_init_and_warm_start():
138
    opt = Hyperactive(X, y)
139
    opt.search(search_config, warm_start=warm_start, scatter_init=10)
140
141
    opt = Hyperactive(X, y)
142
    opt.search(search_config, warm_start=warm_start, scatter_init=10)
143
144
145
def test_warm_start_multiple_jobs():
146
    opt = Hyperactive(X, y)
147
    opt.search(search_config, n_jobs=4, warm_start=warm_start)
148
149
150
def test_warm_start():
151
    opt = Hyperactive(X, y)
152
    opt.search(search_config, n_jobs=1, warm_start=warm_start)
153
154
155
def test_get_search_path():
156
    opt = Hyperactive(X, y, get_search_path=True)
157
    opt.search(search_config)
158
159
    opt = Hyperactive(X, y, optimizer="ParticleSwarm", get_search_path=True)
160
    opt.search(search_config)
161