简体   繁体   English

打印具有类型的对象中的特定值<class 'str'>

[英]Printing specific values in an object with type <class 'str'>

I am running a grid search to obtain the optimal parameters for my Holt-Winters model.我正在运行网格搜索以获得我的 Holt-Winters 模型的最佳参数。

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 [.当我打印 c[0] 时,输出是 [.

If your strings are guaranteed to be python literals then you can use ast.literal_eval :如果您的字符串保证是 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

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?为什么score_model返回这些字符串而不仅仅是列表? You might want to take a look at that instead of solving a problem that probably sould not extist in the first place.您可能想看看它,而不是解决一个最初可能不存在的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印出 __str__() function 以获取列表类型 object 的位置 - Issue printing out __str__() function for location of a list type object 熊猫“类型<class 'str'> ” - Pandas “type of <class 'str'>” 类型错误:对象类型<class 'str'>不能传递给 C 代码 - TypeError: Object type <class 'str'> cannot be passed to C code Object型<class 'str'>无法传递给 C 代码 - 虚拟环境</class> - Object type <class 'str'> cannot be passed to C code - virtual environment Pyspark DataframeType error a: DoubleType can not accept object 'a' in type<class 'str'></class> - Pyspark DataframeType error a: DoubleType can not accept object 'a' in type <class 'str'> 参数验证失败:\\n参数 Filters[0].Values[0] 的类型无效,值:{},类型:<class 'dict'> , 有效类型:<class 'str'> - Parameter validation failed:\nInvalid type for parameter Filters[0].Values[0], value: {}, type: <class 'dict'>, valid types: <class 'str'> 之间有什么区别<class 'str'>和<type 'str'> - What is the difference between <class 'str'> and <type 'str'> 使用__str__打印对象的数组元素 - Using __str__ for printing array elements of an object YAML 在打印对象时使用 repr/str - YAML to use repr/str when printing an object 从对象打印Python __str__ - Printing Python __str__ from an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM