【问题标题】:avoid x is neither increasing nor decreasing : {}.".format(x) when calculate auc score避免 x 既不增加也不减少:{}.".format(x) 计算 auc 分数时
【发布时间】:2021-12-09 22:19:00
【问题描述】:

所以我有一个假阳性率列表和一个通过改变某个阈值获得的真阳性率列表。我正在尝试计算 auc 分数,但不幸的是我不能使用 scikit-learn 中的 roc_auc_score 方法,所以我使用的是更通用的 auc 方法。

这是我的代码:

print(fpr_list)
#[0.4824561403508772, 0.4205607476635514, 0.41037735849056606, 0.391304347826087, 0.35467980295566504, 0.2857142857142857, 0.23195876288659795, 0.20618556701030927, 0.19170984455958548, 0.16753926701570682, 0.12105263157894737, 0.10052910052910052, 0.10052910052910052, 0.09523809523809523, 0.08465608465608465, 0.07936507936507936, 0.058823529411764705, 0.0481283422459893, 0.0427807486631016, 0.03208556149732621, 0.0374331550802139, 0.0213903743315508, 0.0213903743315508, 0.0213903743315508, 0.0213903743315508, 0.016042780748663103, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754, 0.0106951871657754]
print(tpr_list)
#[0.7619047619047619, 0.7619047619047619, 0.7619047619047619, 0.7619047619047619, 0.7523809523809524, 0.7428571428571429, 0.7238095238095238, 0.6952380952380952, 0.6952380952380952, 0.6857142857142857, 0.6761904761904762, 0.6571428571428571, 0.6476190476190476, 0.6476190476190476, 0.638095238095238, 0.638095238095238, 0.6285714285714286, 0.6, 0.6, 0.6, 0.5904761904761905, 0.5904761904761905, 0.580952380952381, 0.580952380952381, 0.5714285714285714, 0.5714285714285714, 0.5714285714285714, 0.5714285714285714, 0.5333333333333333, 0.5333333333333333, 0.5142857142857142, 0.5047619047619047, 0.4952380952380952, 0.4952380952380952, 0.4857142857142857]
print(auc(fpr_list,tpr_list))

当我使用 auc 方法时出现错误

ValueError: x is neither increasing nor decreasing

我理解错误,这是因为在列表中有相等的值,但是没有办法忽略这个错误并仍然计算 auc 分数吗?

【问题讨论】:

    标签: python roc auc


    【解决方案1】:

    您可以使用scipy.integrate 计算曲线下的面积。 但是,您需要对 fpr_list 进行排序,否则 dx 将是负数,您可能会得到 AUC 的负值。

    from scipy import integrate
    import numpy as np
    sorted_index = np.argsort(fpr_list)
    fpr_list_sorted =  np.array(fpr_list)[sorted_index]
    tpr_list_sorted = np.array(tpr_list)[sorted_index]
    integrate.trapz(y=tpr_list_sorted, x=fpr_list_sorted)
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 2019-07-09
      • 2015-07-24
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多