I am running a grid search to obtain the optimal parameters for my Holt-Winters model.
My problem isn't with this, but from splitting the output.
# grid search configs
def grid_search(data, cfg_list, n_test, parallel=False):
scores = None
if parallel:
# execute configs in parallel
executor = Parallel(n_jobs=cpu_count(), backend='multiprocessing')
tasks = (delayed(score_model)(data, n_test, cfg) for cfg in cfg_list)
scores = executor(tasks)
else:
scores = [score_model(data, n_test, cfg) for cfg in cfg_list]
# remove empty results
scores = [r for r in scores if r[1] != None]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
if __name__ == '__main__':
# load dataset
data = series.values
# data split
n_test = 6
# model configs
cfg_list = exp_smoothing_configs(seasonal=[12])
# grid search
scores = grid_search(data, cfg_list, n_test)
print('done')
# list top 3 configs
for cfg, error in scores[:3]:
print(cfg, error)
c=scores[0][0]
print(c)
[None, False, 'add', 12, True, False]
print(type(c))
<class 'str'>
scores looks like this:
print(scores)
[("[None, False, 'add', 12, True, False]", 77287.30870008223), ("[None, False, 'add', 12, True, True]", 82358.18290862873), ("[None, False, 'add', 12, False, False]", 89424.13093850421), ("[None, False, 'add', 12, False, True]", 92702.59197070534), ("[None, False, 'mul', 12, False, False]", 138889.51422419012), ("[None, False, 'mul', 12, False, True]", 143041.76605824768), ("[None, False, 'mul', 12, True, False]", 151094.71387298577), ("[None, False, 'mul', 12, True, True]", 155234.18460656865), ("['add', False, None, 12, True, False]", 286472.4116758029), ("['add', False, None, 12, True, True]", 287519.4400580368), ("['add', True, None, 12, True, False]", 290043.11385268555), ("['add', True, None, 12, True, True]", 291097.96863945096), ("['add', False, None, 12, False, True]", 293015.5612969006), ("['add', False, None, 12, False, False]", 294728.0244142087), ("['mul', False, None, 12, True, True]", 298744.59053811635), ("['add', True, None, 12, False, True]", 298773.6174892717), ("['mul', False, None, 12, False, False]", 299016.44699124835), ("['add', True, None, 12, False, False]", 301282.3881674402), ("['mul', False, None, 12, False, True]", 301539.4816286923), ("['mul', False, None, 12, True, False]", 305131.0375033285), ('[None, False, None, 12, False, False]', 305753.8289179507), ("['mul', True, None, 12, True, True]", 307790.8943120729), ("['mul', True, None, 12, True, False]", 312108.27495445166), ('[None, False, None, 12, True, False]', 313635.56832930725), ('[None, False, None, 12, False, True]', 313762.349260778), ("['mul', True, None, 12, False, True]", 315240.5397127802), ('[None, False, None, 12, True, True]', 315930.0729960225), ("['mul', True, None, 12, False, False]", 319352.33996587264)]
How I can print out the first entry (which is None)? When I print c[0], the output is [.
If your strings are guaranteed to be python literals then you can use ast.literal_eval
:
import ast
c = "[None, False, 'add', 12, True, False]"
assert ast.literal_eval(c)[0] is None
assert ast.literal_eval(c)[2] == 'add'
assert ast.literal_eval(c)[3] == 12
This should solve the problem as stated in the question.
But something does not feel right here. Why is score_model
returning those strings instead of just lists? You might want to take a look at that instead of solving a problem that probably sould not extist in the first place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.