在类型为<class'str'=“”>的对象中打印特定值

时间:2019-02-08 10:40:36

标签: python arrays string tuples

我正在运行网格搜索以获取Holt-Winters模型的最佳参数。

我的问题不在于此,而在于分割输出。

# 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'>

分数看起来像这样:

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)]

我如何打印出第一项(无)?当我打印c [0]时,输出为[。

1 个答案:

答案 0 :(得分:1)

如果确保您的字符串是python文字,则可以使用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

这应该解决问题中所述的问题。

但是这里有些不舒服的感觉。为什么score_model返回这些字符串而不只是列表?您可能想看看它,而不是解决可能根本就不存在的问题。