-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (52 loc) · 2.06 KB
/
utils.py
File metadata and controls
71 lines (52 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from joblib import Parallel
from joblib import delayed
import numpy as np
from multiprocessing import cpu_count
import itertools
def score_model(func, train, fold_size, scoring_func, params, folds):
try:
scores = []
for i in range(folds):
#
split_point=len(train)-(folds-i)*fold_size
#
x=train[:split_point]
f=train[split_point:split_point+fold_size]
smooth_series = func(x, len(f),**params)[0]
# compute the score
forecasted = np.array(smooth_series[-len(f):])
acutals = np.array(f)
scores.append(scoring_func(forecasted,acutals))
return sum(scores)/len(scores)
except ValueError as e:
return np.nan
def grid_search(data, cfg_list, score_lambda, fold_size, folds, parallel=True):
scores = None
if parallel:
# execute configs in parallel
executor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')
tasks = (delayed(score_model)(data, fold_size, score_lambda, cfg, folds) for cfg in cfg_list)
scores = executor(tasks)
else:
scores=[]
for cfg in cfg_list:
scores.append(score_model(data, fold_size, score_lambda, cfg, folds))
return scores
def map_to_configuration_list(in_options):
return [dict(zip(in_options,p)) for p in list(itertools.product(*in_options.values()))]
def handle_scores(scores,cfg_list, print_top = False):
# remove empty results
max_value = np.nanmax(np.array(scores))
for i in range(len(scores)):
if np.isnan(scores[i]):
scores[i]=max_value
# sort configs by error, asc
scores=np.array(scores)
min_index=np.argmin(scores)
if print_top:
print("Min Top 10 is:")
for index in np.argpartition(scores,10)[:10]:
print("----")
print(f"score is {scores[index]}")
print(f"score is {cfg_list[index]}")
return scores[min_index],cfg_list[min_index]