Passed
Pull Request — master (#101)
by Simon
01:37
created

tests_old.test_issues.test_issue_25   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A test_issue_25() 0 56 1
1
import numpy as np
2
import pandas as pd
3
4
from hyperactive import Hyperactive
5
6
7
def test_issue_25():
8
    # set a path to save the dataframe
9
    path = "./search_data.csv"
10
    search_space = {
11
        "n_neighbors": list(range(1, 50)),
12
    }
13
14
    # get para names from search space + the score
15
    para_names = list(search_space.keys()) + ["score"]
16
17
    # init empty pandas dataframe
18
    search_data = pd.DataFrame(columns=para_names)
19
    search_data.to_csv(path, index=False)
20
21
    def objective_function(para):
22
        # score = random.choice([1.2, 2.3, np.nan])
23
        score = np.nan
24
25
        # you can access the entire dictionary from "para"
26
        parameter_dict = para.para_dict
27
28
        # save the score in the copy of the dictionary
29
        parameter_dict["score"] = score
30
31
        # append parameter dictionary to pandas dataframe
32
        search_data = pd.read_csv(path, na_values="nan")
33
        search_data_new = pd.DataFrame(
34
            parameter_dict, columns=para_names, index=[0]
35
        )
36
37
        # search_data = search_data.append(search_data_new)
38
        search_data = pd.concat(
39
            [search_data, search_data_new], ignore_index=True
40
        )
41
42
        search_data.to_csv(path, index=False, na_rep="nan")
43
44
        return score
45
46
    hyper0 = Hyperactive()
47
    hyper0.add_search(objective_function, search_space, n_iter=50)
48
    hyper0.run()
49
50
    search_data_0 = pd.read_csv(path, na_values="nan")
51
    """
52
    the second run should be much faster than before, 
53
    because Hyperactive already knows most parameters/scores
54
    """
55
    hyper1 = Hyperactive()
56
    hyper1.add_search(
57
        objective_function,
58
        search_space,
59
        n_iter=50,
60
        memory_warm_start=search_data_0,
61
    )
62
    hyper1.run()
63