from sklearn.metrics import *
# 參考鏈接
# https://blog.csdn.net/shine19930820/article/details/78335550
# https://blog.csdn.net/weixin_41770169/article/details/79547972
# y_true表示真實標籤,y_pre表示預測標籤,y_pro表示預測概率
y_true = [1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0]
y_pre = [1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0]
y_pro = [0.7, 0.3, 0.9, 0.5, 0.7, 0.4, 0.6, 0.3, 0.8, 0.5, 0.6, 0.3, 0.8, 0.8, 0.7, 0.6, 0.6, 0.9, 0.7, 0.7]
# 混淆矩陣
# ━━━━━━━━━━━━━━━━━━━
# predict result
# ┏━━━━━━━━━━━━━━━━━━━┓
# ┃ 1 -1 ┃
# ┣━━━━━━━━━━━━━━━━━━━┫
# ┃ 1 7 2 ┃
# real result ┃ ┃
# ┃-1 1 10 ┃
# ┗━━━━━━━━━━━━━━━━━━━┛
#
# 準確率、auc、召回率、正確率和F1值基於混淆矩陣的評估度量
'''-----------------------------------------------------------'''
# 1.分類評價指標
# 1.1對數損失(Log-loss)
# 計算公式:對數損失值 = 0.847053621126
print('對數損失:', log_loss(y_true, y_pro))
# 1.2auc(roc曲線下的面積,只限制於2分類)
# 計算公式:roc曲線面積 = 0.843434343434
print('auc值:', roc_auc_score(y_true, y_pre))
# 1.3準確率(Accuracy)
# 計算公式:(7 + 10) / (1 + 2 + 7 + 10) = 0.85
print('準確率:', accuracy_score(y_true, y_pre))
# 1.4召回率(Recall)
# 計算公式:7 / (2 + 7) = 0.777777777778
print('召回率:', recall_score(y_true, y_pre))
# 1.5精確率(Precision)
# 計算公式:7 / 8 = 0.875
print('正確率:', precision_score(y_true, y_pre))
# 1.6F1值(F1-score)
# 計算公式:2 * 0.777777777778 * 0.875 / (0.777777777778 + 0.875) = 0.823529411765
print('F1值:', f1_score(y_true, y_pre))
'''-----------------------------------------------------------'''
# 2.迴歸評價指標
# 2.1解釋方差,越接近1越好
print('可釋方差值:', explained_variance_score(y_true, y_pre))
# 2.2平均絕對誤差,越小越好
print('平均絕對誤差:', mean_absolute_error(y_true, y_pre))
# 2.3均方誤差,越小越好
print('均方誤差:', mean_squared_error(y_true, y_pre))
# 2.4中值絕對誤差,越小越好
print('中值絕對誤差:', median_absolute_error(y_true, y_pre))
# 2.5R2方值,越接近1越好
print('R2方值:', r2_score(y_true, y_pre))