AUC ROC

AUC ROC
AUC ROC

In [1]: import numpy as np
In [2]: from sklearn.metrics import roc_auc_score
In [3]: y_true = np.array([0, 0, 1, 1])
In [4]: y_scores = np.array([0.1, 0.4, 0.35, 0.8])
In [5]: roc_auc_score(y_true, y_scores)
Out[5]: 0.75

阈值为:0.1时,点(1,1) (fpr,tpr)
阈值为:0.35时,点(0.5,1)
阈值为:0.4时,点(0.5,0.5)
阈值为:0.8时,点(0,0.5)

In [6]: import numpy as np
In [7]: from sklearn import metrics
In [8]: y = np.array([1,1,2,2]) ## 没有"0"
In [9]: scores = np.array([0.1,0.4,0.35,0.8])
In [10]: fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
In [11]: fpr
Out[11]: array([0. , 0. , 0.5, 0.5, 1. ])
In [12]: tpr
Out[12]: array([0. , 0.5, 0.5, 1. , 1. ]
In [13]: thresholds
Out[13]: array([1.8 , 0.8 , 0.4 , 0.35, 0.1 ])

AUC ROC

In [1]: y = [2,2,1,2,2,2,1,1,2,1,2,1,2,1,1,1,2,1,2,1]

In [2]: len(y)
Out[2]: 20

In [3]: x = [0.9,.8,.7,.6,.55,.54,.53,.52,.51,.505,.4,.39,.38,.37,.36,.35,.34,.33,.3,.1]

In [4]: len(x)
Out[4]: 20

In [5]: import numpy as np

In [6]: y_label = np.array(y)

In [7]: x_scores = np.array(x)

In [8]: from sklearn import metrics

In [9]: fpr, tpr, thresholds = metrics.roc_curve(y_label, x_scores,pos_label=2)

In [10]: fpr
Out[10]:
array([0. , 0. , 0. , 0.1, 0.1, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.8, 0.8,
       0.9, 0.9, 1. ])

In [11]: tpr
Out[11]:
array([0. , 0.1, 0.2, 0.2, 0.5, 0.5, 0.6, 0.6, 0.7, 0.7, 0.8, 0.8, 0.9,
       0.9, 1. , 1. ])

In [12]: thresholds
Out[12]:
array([1.9  , 0.9  , 0.8  , 0.7  , 0.54 , 0.52 , 0.51 , 0.505, 0.4  ,
       0.39 , 0.38 , 0.35 , 0.34 , 0.33 , 0.3  , 0.1  ])

In [13]: import matplotlib.pyplot as plt

In [14]: plt.plot(fpr, tpr)
Out[14]: [<matplotlib.lines.Line2D at 0x2b1d1f2f0b8>]

In [15]: plt.show()

AUC ROC