官方地址:点击打开链接
形式:sklearn.metrics.
auc
(x, y, reorder=False)
规则:利用梯形法则计算曲线下的面积(AUC)。
Parameters: | x : array, shape = [n]
y : array, shape = [n]
reorder : boolean, optional (default=False)
|
Returns: | auc : float |
已经得到了FPR.TPR,由此就得到了横纵坐标,然后用高数中学习的计算曲面面积的方法,也就梯度法(一种积分方法来计算曲线下面积)
import numpy as np
from sklearn import metrics
y = np.array([1, 1, 2, 2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
print(fpr,'\n',tpr,'\n',thresholds)
这个比较简单,绘图如下:
就是计算黄色部分的面积,口算都可得到AUC=0.75
但是复杂的数据还是要用标准的算法,详细过程可以参考博客,写的很好:点击打开链接
只想知道怎么使用,不用知道原理的,计算代码如下:
import numpy as np
from sklearn import metrics
y = np.array([1, 1, 2, 2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, scores, pos_label=2)
print(fpr,'\n',tpr,'\n',thresholds)
print(metrics.auc(fpr,tpr))