(Area Under the Curve)可以通过以下步骤实现:
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 1, 1, 0, 1])
y_pred = np.array([0.2, 0.6, 0.8, 0.3, 0.9])
sorted_indices = np.argsort(y_pred)
positive_ranks = np.sum(y_true[sorted_indices] == 1)
negative_ranks = np.sum(y_true[sorted_indices] == 0)
auc = (positive_ranks - (positive_ranks * (positive_ranks + 1)) / 2) / (positive_ranks * negative_ranks)
print("手工计算的AUC值为:", auc)
完整代码如下:
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 1, 1, 0, 1])
y_pred = np.array([0.2, 0.6, 0.8, 0.3, 0.9])
sorted_indices = np.argsort(y_pred)
positive_ranks = np.sum(y_true[sorted_indices] == 1)
negative_ranks = np.sum(y_true[sorted_indices] == 0)
auc = (positive_ranks - (positive_ranks * (positive_ranks + 1)) / 2) / (positive_ranks * negative_ranks)
print("手工计算的AUC值为:", auc)
AUC是评估二分类模型性能的常用指标,它代表了ROC曲线下的面积,范围在0到1之间,值越大表示模型性能越好。AUC值为0.5时表示模型性能等同于随机猜测,大于0.5时表示模型优于随机猜测。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云