第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 Python 字典列表中訪問某些鍵

如何從 Python 字典列表中訪問某些鍵

慕桂英546537 2023-07-27 15:51:16
我正在使用多種評分方法來處理 cross_validate。該results變量是一個字典列表。我正在嘗試使用不同模型訪問 F1 分?jǐn)?shù),results['test_f1_score']但我得到了TypeError: list indices must be integers or slices, not tuple. 從列表中獲取 F1 分?jǐn)?shù)的最佳方法是什么?這是從 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 回答

?
炎炎設(shè)計

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超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]


查看完整回答
反對 回復(fù) 2023-07-27
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個贊

使用列表理解來檢查列表項,并獲取它的每個字典'test_f1_score'

  1. 查看結(jié)果:for item in results

  2. 驗(yàn)證當(dāng)前結(jié)果以“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])]


查看完整回答
反對 回復(fù) 2023-07-27
  • 2 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號