Skip to content

Instantly share code, notes, and snippets.

@MagicUmom
Created August 24, 2023 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagicUmom/4fc722acbda7054b5031c549215c7b2e to your computer and use it in GitHub Desktop.
Save MagicUmom/4fc722acbda7054b5031c549215c7b2e to your computer and use it in GitHub Desktop.
weighted_Fscore.py
def weighted_Fscore(y_true, y_pred):
Beta_1 = 0.5
Beta_2 = 0.125
Beta_3 = 0.125
N_tu = 0
N_td = 0
N_tf = 0
E_wutd = 0
E_wdtu = 0
E_wutf = 0
E_wdtf = 0
E_wftu = 0
E_wftd = 0
assert(len(y_true) == len(y_pred)), "len(y_true) 不等於 len(y_pred)"
for i in range(len(y_true)):
true = y_true[i]
pred = y_pred[i]
if true == 0:
if pred ==0:
N_tf+=1
elif pred==1:
E_wutf+=1
elif pred==2:
E_wdtf+=1
elif true == 1:
if pred ==0:
E_wftu+=1
elif pred==1:
N_tu+=1
elif pred==2:
E_wdtu+=1
elif true == 2:
if pred ==0:
E_wftd+=1
elif pred==1:
E_wutd+=1
elif pred==2:
N_td+=1
N_tp = N_tu + N_td + (Beta_3*Beta_3)*N_tf
E_1st = E_wutd + E_wdtu
E_2nd = E_wutf + E_wdtf
E_3rd = E_wutf + E_wftd
print(N_tp, N_tu, N_td, N_tf)
print(E_1st , E_wutd , E_wdtu)
print(E_2nd , E_wutf , E_wdtf)
print(E_3rd , E_wftu , E_wftd)
return (1+ Beta_1*Beta_1 + Beta_2*Beta_2 )*N_tp / ((1+ Beta_1*Beta_1 + Beta_2*Beta_2 )*N_tp + E_1st + Beta_1*Beta_1*E_2nd + Beta_2*Beta_2*E_3rd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment