我正在比較多個音頻檔案中 IBM Watson 文本與語音的基線置信度。我可以使用訪問單個記錄的置信度,pprint(data_response['results'][0]['alternatives'][0]['confidence'])但不能回傳多個置信度。我需要計算整個成績單的平均置信度。我已經研究過迭代嵌套的字典,但到目前為止我讀過的所有地方都說只回傳鍵而不是值。
應該使用什么方法來獲得所有置信水平的平均值?
這是使用漂亮列印的嵌套字典的樣子:
{'result_index': 0,
'results': [{'alternatives': [{'confidence': 0.99, 'transcript': 'hello '}],
'final': True},
{'alternatives': [{'confidence': 0.9,
'transcript': 'good morning any this is '}],
'final': True},
{'alternatives': [{'confidence': 0.59,
'transcript': "I'm on a recorded morning "
'%HESITATION today start running '
"yeah it's really good how are "
"you %HESITATION it's one three "
'six thank you so much for '
'asking '}],
'final': True},
{'alternatives': [{'confidence': 0.87,
'transcript': 'I appreciate this opportunity '
'to get together with you and '
'%HESITATION you know learn more '
'about you your interest in '}],
'final': True},
uj5u.com熱心網友回復:
您可以使用statistics.mean來計算所有置信水平的平均值:
from statistics import mean
data_response = {
"result_index": 0,
"results": [
{
"alternatives": [{"confidence": 0.99, "transcript": "hello "}],
"final": True,
},
{
"alternatives": [
{"confidence": 0.9, "transcript": "good morning any this is "}
],
"final": True,
},
{
"alternatives": [
{
"confidence": 0.59,
"transcript": "I'm on a recorded morning "
"%HESITATION today start running "
"yeah it's really good how are "
"you %HESITATION it's one three "
"six thank you so much for "
"asking ",
}
],
"final": True,
},
{
"alternatives": [
{
"confidence": 0.87,
"transcript": "I appreciate this opportunity "
"to get together with you and "
"%HESITATION you know learn more "
"about you your interest in ",
}
],
"final": True,
},
],
}
m = mean(
a["confidence"] for r in data_response["results"] for a in r["alternatives"]
)
print(m)
印刷:
0.8375
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/317941.html
