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

How to access certain keys from a list of dictionaries in Python

我正在 cross_validate 使用多种评分方法。 results 变量是一个字典列表。我正在尝试使用 results['test_f1_score'] 访问不同模型的 F1 分数,但我得到的是 TypeError: list indices must be integers or slices, not tuple。从列表中获取 F1 分数的最佳方法是什么?

这是从结果变量生成的列表:

[{'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.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]),
  '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,
         0.42857143, 0.5       , 0.28571429, 0.42857143, 0.57142857,
         0.85714286, 0.42857143, 0.21428571, 0.57142857, 0.28571429,
         0.64285714, 0.42857143, 0.64285714, 0.35714286, 0.42857143,
         0.57142857, 0.42857143, 0.71428571, 0.42857143, 0.64285714,
         0.57142857, 0.57142857, 0.28571429, 0.35714286, 0.5       ]),
  'test_specificity': array([0.96783961, 0.9789689 , 0.94263502, 0.96743044, 0.97872166,
         0.98248629, 0.98355021, 0.9400933 , 0.98977003, 0.92937229,
         0.94983633, 0.97716858, 0.9898527 , 0.98461538, 0.98690564,
         0.98387757, 0.95662493, 0.93403716, 0.97724855, 0.97823062,
         0.91636661, 0.98436989, 0.97430442, 0.98477905, 0.98264997,
         0.97479335, 0.92585318, 0.97233816, 0.93518291, 0.94205745])},
 {'fit_time': array([266.90850329, 276.56025481, 286.16302919, 277.08430457,
         286.8862102 , 288.08904719, 288.04218554, 272.11777163,
         275.47355413, 272.80465865, 277.46604395, 264.88494945,
         276.60775948, 279.0381875 , 277.55336189, 279.63098645,
         259.44465542, 258.57707739, 262.15785336, 263.71264052,
         270.20684457, 269.78548741, 269.23725843, 265.4925046 ,
         221.50381541, 220.98780727, 224.46779513, 212.47151136,
         219.48664856, 215.95075083]),
  'score_time': array([10.18801832,  8.01537871,  3.33346009,  7.78975368,  2.98581243,
          2.70479202,  2.86861777,  8.02667737,  9.41659141, 10.13697696,
          6.73046923,  9.69650173,  4.27995682,  3.4434793 ,  3.63535523,
          4.40075231, 10.22509551, 10.44048429,  9.14734888,  7.9576447 ,
          3.13996601,  4.10830784,  2.80078673,  2.92972565,  6.48999166,
          6.47848701,  3.41430521,  6.733845  ,  1.51497555,  2.96808743]),
  'test_f1_score': 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]),
  'test_f05_measure': array([0.76086957, 0.96774194, 0.98484848, 0.83333333, 0.92857143,
         0.92592593, 0.96774194, 0.92592593, 0.96774194, 0.92857143,
         0.83333333, 0.86956522, 0.86206897, 0.98484848, 0.94827586,
         0.90909091, 0.96774194, 0.96774194, 0.90909091, 0.94827586,
         0.92592593, 0.90909091, 0.94594595, 0.94827586, 0.98484848,
         0.94827586, 0.96774194, 0.83333333, 0.92592593, 0.83333333]),
  'test_sensitivity': array([0.5       , 0.85714286, 0.92857143, 0.64285714, 0.92857143,
         0.71428571, 0.85714286, 0.71428571, 0.85714286, 0.92857143,
         0.64285714, 0.57142857, 0.71428571, 0.92857143, 0.78571429,
         0.85714286, 0.85714286, 0.85714286, 0.85714286, 0.78571429,
         0.71428571, 0.85714286, 1.        , 0.78571429, 0.92857143,
         0.78571429, 0.85714286, 0.64285714, 0.71428571, 0.5       ]),
  'test_specificity': array([0.99991817, 1.        , 1.        , 0.99991817, 0.99991816,
         1.        , 1.        , 1.        , 1.        , 0.99991816,
         0.99991817, 1.        , 0.99991817, 1.        , 1.        ,
         0.99991816, 1.        , 1.        , 0.99991816, 1.        ,
         1.        , 0.99991817, 0.99991817, 1.        , 1.        ,
         1.        , 1.        , 0.99991816, 1.        , 1.        ])},
 {'fit_time': array([174.20313787, 198.07899594, 184.28722906, 178.60294867,
         199.02107191, 179.50637341, 174.12752724, 178.98297739,
         154.63025308, 167.98617458, 178.08862185, 168.87556529,
         154.76291323, 163.30928159, 142.50800228, 165.50385952,
         168.07484317, 161.5811913 , 157.46719265, 153.52208304,
         171.98308134, 158.28413796, 141.83335066, 168.22751117,
         143.11924505, 133.29637599, 141.15710378, 150.20137   ,
         149.12544584, 130.72285938]),
  'score_time': array([10.3494947 , 18.17751551,  7.69723105, 14.2464962 , 19.54308391,
          7.40629816, 13.88110065, 14.76199102, 11.11700416,  9.4580555 ,
         12.19720626, 11.57381821, 15.72216558, 10.20245957, 10.69855881,
         13.65211844, 14.3795464 , 16.48296714, 10.02136064, 11.43304801,
         15.07670116, 12.36216903,  9.41213584, 11.64626813,  8.78664351,
         11.56165099,  8.66782999,  7.65322256,  8.58487797,  8.68105125]),
  'test_f1_score': 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]),
  'test_f05_measure': array([0.0006721 , 0.00042731, 0.00059321, 0.00114647, 0.00157841,
         0.00071875, 0.00040591, 0.00063781, 0.00118381, 0.00152567,
         0.00157398, 0.00144697, 0.00100961, 0.00071915, 0.00103029,
         0.00163812, 0.00133618, 0.00037988, 0.00042499, 0.00119904,
         0.00078425, 0.00102194, 0.00115816, 0.00108951, 0.00182432,
         0.00092328, 0.00224316, 0.00039714, 0.00081706, 0.00067892]),
  'test_sensitivity': array([0.21428571, 0.14285714, 0.21428571, 0.35714286, 0.57142857,
         0.28571429, 0.07142857, 0.21428571, 0.42857143, 0.57142857,
         0.64285714, 0.28571429, 0.35714286, 0.21428571, 0.42857143,
         0.5       , 0.35714286, 0.07142857, 0.14285714, 0.21428571,
         0.28571429, 0.21428571, 0.35714286, 0.35714286, 0.57142857,
         0.28571429, 0.42857143, 0.07142857, 0.28571429, 0.21428571]),
  'test_specificity': array([0.54394435, 0.52168576, 0.48322422, 0.55458265, 0.48244537,
         0.43129552, 0.74834274, 0.5193551 , 0.48228169, 0.46452247,
         0.41612111, 0.71783961, 0.49410802, 0.57381342, 0.40502496,
         0.56371225, 0.61789017, 0.73107456, 0.51902774, 0.74457812,
         0.47888707, 0.7002455 , 0.55908347, 0.53126023, 0.55233653,
         0.55741059, 0.72714625, 0.74277764, 0.4997954 , 0.54849006])}]

您必须遍历您的列表:

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]

使用列表理解遍历列表项,并为每个字典获取它 '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])]