【机器学习&深度学习】预测模型最合适阈值Cutoff选取及其它指标计算(Python版本)

绘制ROC曲线,基于ROC曲线上各点的约登指数计算最佳cutoff,从而计算其它指标

from sklearn.metrics import roc_auc_score, f1_score, accuracy_score, recall_score, precision_score, roc_curve, confusion_matrix
from _collections import OrderedDict
import numpy as np

def evaluate(y_true, y_pred, digits=4, cutoff='auto'):
    '''
    calculate several metrics of predictions

    Args:
        y_true: list, labels
        y_pred: list, predictions
        digits: The number of decimals to use when rounding the number. Default is 4
        cutoff: float or 'auto'

    Returns:
        evaluation: dict

    '''

    if cutoff == 'auto':
        fpr, tpr, thresholds = roc_curve(y_true, y_pred)
        youden = tpr-fpr
        cutoff = thresholds[np.argmax(youden)]

    y_pred_t = [1 if i > cutoff else 0 for i in y_pred]

    evaluation = OrderedDict()
    tn, fp, fn, tp = confusion_matrix(y_true, y_pred_t).ravel()
    evaluation['auc'] = round(roc_auc_score(y_true, y_pred), digits)
    evaluation['acc'] = round(accuracy_score(y_true, y_pred_t), digits)
    evaluation['recall'] = round(recall_score(y_true, y_pred_t), digits)
    evaluation['specificity'] = round(tn / (tn + fp), digits)
    evaluation['F1'] = round(f1_score(y_true, y_pred_t), digits)
    evaluation['cutoff'] = cutoff

    return evaluation
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于机器学习深度学习模型的性能评估,我们可以从以下几个方面进行综合评估: 1. 准确率(Accuracy):衡量模型对于所有样本的分类/预测的正确率。 2. 精确率(Precision):衡量模型对真实类别为正类的样本,分类结果为正类的概率,即 TP / (TP + FP)。 3. 召回率(Recall):衡量模型对于真实类别为正类的样本,被分类为正类的概率,即 TP / (TP + FN)。 4. F1-Score:衡量模型精确率与召回率的平衡,其为 2 * Precision * Recall / (Precision + Recall)。 5. ROC曲线(Receiver Operating Characteristic Curve):根据不同的样本阈值,绘制真正率(True Positive Rate)和假正率(False Positive Rate)之间的关系曲线。 6. AUC(Area Under Curve):ROC曲线下面积,该指标用来度量分类器的质量,其在 0.5~1 之间,取越接近 1,则分类器效果越好。 7. 交叉验证:在样本不充足时,可以采用交叉验证的方式对模型进行评估。交叉验证通常是将样本分为若干份,每份分别作为训练集和测试集,多次进行训练和测试,最终得到平均表现。 8. 模型复杂度:可以通过模型参数数量、计算复杂度等方面来评估模型的复杂程度。应当避免使用复杂度很高的模型,在数据充足的情况下,应当优先选择简单模型,以避免过拟合和模型训练时间的过长。 通过综合以上这些方面的评估,选择最合适的模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值