1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
class Memory: |
6
|
|
|
def __init__(self): |
7
|
|
|
current_path = os.path.realpath(__file__) |
|
|
|
|
8
|
|
|
meta_learn_path, _ = current_path.rsplit("/", 1) |
9
|
|
|
|
10
|
|
|
self.meta_data_path = meta_learn_path + "/meta_data/" |
11
|
|
|
|
12
|
|
|
def _get_opt_meta_data(self, _cand_, X, y): |
13
|
|
|
results_dict = {} |
14
|
|
|
para_list = [] |
15
|
|
|
score_list = [] |
16
|
|
|
|
17
|
|
|
for key in _cand_._space_.memory.keys(): |
18
|
|
|
pos = np.fromstring(key, dtype=int) |
|
|
|
|
19
|
|
|
para = _cand_._space_.pos2para(pos) |
20
|
|
|
score = _cand_._space_.memory[key] |
21
|
|
|
|
22
|
|
|
if score != 0: |
23
|
|
|
para_list.append(para) |
24
|
|
|
score_list.append(score) |
25
|
|
|
|
26
|
|
|
results_dict["params"] = para_list |
27
|
|
|
results_dict["mean_test_score"] = score_list |
28
|
|
|
|
29
|
|
|
return results_dict |
30
|
|
|
|
31
|
|
|
def collect(self, X, y, _cand_): |
32
|
|
|
results_dict = self._get_opt_meta_data(_cand_, X, y) |
33
|
|
|
|
34
|
|
|
para_pd = pd.DataFrame(results_dict["params"]) |
|
|
|
|
35
|
|
|
md_model = para_pd.reindex(sorted(para_pd.columns), axis=1) |
36
|
|
|
|
37
|
|
|
metric_pd = pd.DataFrame( |
38
|
|
|
results_dict["mean_test_score"], columns=["mean_test_score"] |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
md_model = pd.concat([para_pd, metric_pd], axis=1, ignore_index=False) |
42
|
|
|
|
43
|
|
|
return md_model |
44
|
|
|
|
45
|
|
|
def _get_hash(self, object): |
46
|
|
|
return hashlib.sha1(object).hexdigest() |
|
|
|
|
47
|
|
|
|
48
|
|
|
def _get_func_str(self, func): |
49
|
|
|
return inspect.getsource(func) |
|
|
|
|
50
|
|
|
|
51
|
|
|
def _get_file_path(self, X_train, y_train, model_func): |
52
|
|
|
func_str = self._get_func_str(model_func) |
53
|
|
|
feature_hash = self._get_hash(X_train) |
54
|
|
|
label_hash = self._get_hash(y_train) |
55
|
|
|
|
56
|
|
|
self.func_path = self._get_hash(func_str.encode("utf-8")) + "/" |
57
|
|
|
|
58
|
|
|
directory = self.meta_data_path + self.func_path |
59
|
|
|
if not os.path.exists(directory): |
|
|
|
|
60
|
|
|
os.makedirs(directory) |
61
|
|
|
|
62
|
|
|
return directory + ( |
63
|
|
|
"metadata" |
64
|
|
|
+ "__feature_hash=" |
65
|
|
|
+ feature_hash |
66
|
|
|
+ "__label_hash=" |
67
|
|
|
+ label_hash |
68
|
|
|
+ "__.csv" |
69
|
|
|
) |
70
|
|
|
|