为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 Python 字典列表中访问某些键

如何从 Python 字典列表中访问某些键

慕桂英546537 2023-07-27 15:51:16
我正在使用多种评分方法来处理 cross_validate。该results变量是一个字典列表。我正在尝试使用不同模型访问 F1 分数,results['test_f1_score']但我得到了TypeError: list indices must be integers or slices, not tuple. 从列表中获取 F1 分数的最佳方法是什么?这是从 results 变量生成的列表:[{'fit_time': array([3.25437379, 3.31360006, 4.55807948, 4.13171291, 4.19844866,         5.00323677, 4.02107215, 4.18458176, 4.82133555, 4.25998092,         3.76690507, 3.97073889, 4.82378697, 4.32887745, 4.66021585,         4.54414296, 5.0830574 , 4.85126257, 5.40511918, 5.04520845,         5.11681008, 4.93635201, 4.74664378, 5.08661175, 4.32240748,         4.10375977, 4.31480002, 4.04965734, 3.78688979, 3.88911653]),  'score_time': array([0.14319539, 0.12677526, 0.10101032, 0.09385777, 0.06942415,         0.11760497, 0.06548786, 0.05601525, 0.07890821, 0.07988477,         0.06272411, 0.07337427, 0.07825518, 0.04193068, 0.04330111,         0.04155302, 0.05201483, 0.0699327 , 0.05248165, 0.04978824,         0.05156302, 0.05214596, 0.07210374, 0.06283927, 0.04857302,         0.05679178, 0.04323983, 0.04890585, 0.04407573, 0.05073118]),  'test_f1_score': array([0.03381643, 0.06428571, 0.01939058, 0.02870813, 0.05673759,         0.05128205, 0.06306306, 0.01066667, 0.08275862, 0.0180791 ,         0.03755869, 0.04013378, 0.04255319, 0.07619048, 0.04494382,         0.0818118, 0.02181818, 0.02171291, 0.03367003, 0.04195804,         0.01532567, 0.05687204, 0.0591716 , 0.05825243, 0.07659574,         0.04848485, 0.01724138, 0.02247191, 0.01233046, 0.01920439]),  'test_f05_measure': array([0.02168525, 0.04174397, 0.01229796, 0.01840491, 0.03683241,         0.03355705, 0.04137116, 0.00676133, 0.05576208, 0.01143511,         0.02386635, 0.02599653, 0.02873563, 0.05012531, 0.02985075,         0.05369928, 0.01390176, 0.01374465, 0.02181501, 0.02722323,         0.00967586, 0.03740648, 0.03816794, 0.03836317, 0.05011136,         0.0312989 , 0.01089918, 0.01447178, 0.00780762, 0.01217815]),  'test_sensitivity': array([0.5       , 0.64285714, 0.5       , 0.42857143, 0.57142857,
查看完整描述

2 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

您必须迭代您的列表:


def get_values(res, k)

  out = []

  for v in res:

    if k in v

      out.append(v[k])

  return out


out = get_values(results, 'test_f1_score')

或者单行:


out = [v['test_f1_score'] for v in results if 'test_f1_score' in v]


查看完整回答
反对 回复 2023-07-27
?
茅侃侃

TA贡献1842条经验 获得超21个赞

使用列表理解来检查列表项,并获取它的每个字典'test_f1_score'

  1. 查看结果:for item in results

  2. 验证当前结果以“test_f1_score”为键:if 'test_f1_score' in item.keys()
    2.1。如果它有“test_f1_score”,请将其添加到列表中:item['test_f1_score']

f1_scores = [item['test_f1_score'] for item in results if 'test_f1_score' in item.keys()]

输出:

[array([0.03381643, 0.06428571, 0.01939058, 0.02870813, 0.05673759,

       0.05128205, 0.06306306, 0.01066667, 0.08275862, 0.0180791 ,

       0.03755869, 0.04013378, 0.04255319, 0.07619048, 0.04494382,

       0.08181818, 0.02181818, 0.02171291, 0.03367003, 0.04195804,

       0.01532567, 0.05687204, 0.0591716 , 0.05825243, 0.07659574,

       0.04848485, 0.01724138, 0.02247191, 0.01233046, 0.01920439]), array([0.63636364, 0.92307692, 0.96296296, 0.75      , 0.92857143,

       0.83333333, 0.92307692, 0.83333333, 0.92307692, 0.92857143,

       0.75      , 0.72727273, 0.8       , 0.96296296, 0.88      ,

       0.88888889, 0.92307692, 0.92307692, 0.88888889, 0.88      ,

       0.83333333, 0.88888889, 0.96551724, 0.88      , 0.96296296,

       0.88      , 0.92307692, 0.75      , 0.83333333, 0.66666667]), array([0.00107335, 0.00068248, 0.00094757, 0.00183083, 0.00252127,

       0.00114827, 0.00064725, 0.00101868, 0.00189095, 0.00243717,

       0.00251467, 0.00230814, 0.00161264, 0.00114833, 0.00164609,

       0.00261584, 0.00213311, 0.00060588, 0.00067877, 0.00191205,

       0.00125274, 0.00163043, 0.00184945, 0.00174004, 0.00291333,

       0.00147438, 0.00357782, 0.00063331, 0.00130506, 0.00108421])]


查看完整回答
反对 回复 2023-07-27
  • 2 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信