机器学习 评价指标

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))

猜你喜欢

转载自blog.csdn.net/zh11403070219/article/details/82026338