注:本文是人工智能研究網的學習筆記
ROC是什么
二元分類器(binary classifier)的分類結果
ROC空間
最好的預測模型在左上角,代表100%的靈敏度和0%的虛警率,被稱為完美分類器。
一個隨機猜測模型。會給出從左下角到右上角的沿着對角線的點(對角線被稱作line of no-discrimation)。
對角線上的的點代表了好的分配結果,對角線以下的點代表不好的分配結果,但是可以通過翻轉變成好的分類器。
繪制ROC曲線
AUC--ROC曲線下的面積
當曲線差不多時,求面積, 新的研究表名,AUC曲線存在一些問題。
roc_curve模塊
metrics.roc_curve(y_true, y_score, pos_label=None, sample_weight=None, drop_intermediate=True)
參數:
- y_true: 真正的二元類標簽,如果不是二元分類,需要使用pos_label顯式的指明。
- y_score: 預測得分
- pos_label: 多分類的時候,指定將哪一個看成正樣本,默認是None。
- drop_intermediate: 指定是否丟棄一些次優的閾值,將不會再roc曲線上顯示。曲線較多的時候,可以使用。
返回值:
- fpr:array,shape=[>2]不斷增長的虛警率(假正率)
- tpr:array,shape=[>2]不斷增長的真正率
- thresholds:array,shape=[n_thresholds]閾值
from sklearn.metrics import roc_curve,roc_auc_score
y = np.array([1,1,2,2])
scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y, scores, pos_label=2)
print(fpr)
print(tpr)
print(thresholds)