我有2個函數可以給出精度和召回分數,我需要在使用這兩個分數的同一個庫中定義一個諧波平均函數。功能如下:python函數中的諧波含義?
這裏的功能是:
def precision(ref, hyp):
"""Calculates precision.
Args:
- ref: a list of 0's and 1's extracted from a reference file
- hyp: a list of 0's and 1's extracted from a hypothesis file
Returns:
- A floating point number indicating the precision of the hypothesis
"""
(n, np, ntp) = (len(ref), 0.0, 0.0)
for i in range(n):
if bool(hyp[i]):
np += 1
if bool(ref[i]):
ntp += 1
return ntp/np
def recall(ref, hyp):
"""Calculates recall.
Args:
- ref: a list of 0's and 1's extracted from a reference file
- hyp: a list of 0's and 1's extracted from a hypothesis file
Returns:
- A floating point number indicating the recall rate of the hypothesis
"""
(n, nt, ntp) = (len(ref), 0.0, 0.0)
for i in range(n):
if bool(ref[i]):
nt += 1
if bool(hyp[i]):
ntp += 1
return ntp/nt
將調和平均數功能是什麼樣的? 我只有這一點,但我知道它不是正確的:
def hmean(*args):
return len(args)/sum(1./val for val in args)
要計算的precision
和recall
,利用調和平均數:
def F1(precision, recall):
(2*precision*recall)/(precision+recall)
你能解釋一下爲什麼你認爲它不對嗎?當我嘗試運行程序時,我可能會幫助.. –
我得到了TypeError:無法乘以類型'list'的非int的序列 – marth17
您能告訴我們您如何使用F1功能嗎?看起來像你傳遞的參數是不正確的。特別是'F1'的兩個參數必須是數字。 –