【机器学习&深度学习】预测模型最合适阈值Cutoff选取及其它指标计算(Python版本)
绘制ROC曲线,基于ROC曲线上各点的约登指数计算最佳cutoff,从而计算其它指标
·
绘制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
更多推荐
已为社区贡献1条内容
所有评论(0)