Precision、Recall、P-R曲線、ROC、AUC、mAP

Github對應博客

1. Precision和Recall

對於二分類問題,可將樣例 根據其真實類別與學習器預測類別的組合劃分爲:

  • TP (True Positive): 預測爲真,實際爲真
  • FP (False Positive): 預測爲真,實際爲假
  • TN(True Negative): 預測爲假,實際爲假
  • FN (False Negative):預測爲假,實際爲真

令 TP、FP、TN、FN分別表示其對應的樣例數,則顯然有 TP + FP + TN + FN = 樣例總數
分類結果的 “混淆矩陣” 如下:

實際爲真 T 實際爲假 F
預測爲正例 P TP (預測爲1,實際爲1) FP (預測爲1,實際爲0)
預測爲負例 N FN (預測爲0,實際爲1) TN (預測爲0,實際爲0)

Precision=TPTP+FP (查準率)Precision = \frac{TP}{TP + FP} \\ \\ \\ {\color{Purple}{(預測的好瓜中有多少是真的好瓜)}}

Recall=TPTP+FN (查全率)Recall = \frac{TP}{TP + FN} \\ \\ {\color{Purple}{(所有真正的好瓜中有多少被真的挑出來了)}}

2. P-R曲線

一般來說,查準率高時,查全率往往偏低,而查全率高時,查準率往往偏低。通常只有在一些簡單任務中,纔可能使得查全率和查準率都很高。在很多情況,我們可以根據學習器的預測結果,得到對應預測的 confidence scores 得分(有多大的概率是正例),按照得分對樣例進行排序,排在前面的是學習器認爲”最可能“是正例的樣本,排在最後的則是學習器認爲“最不可能”是正例的樣本。每次選擇當前第 ii 個樣例的得分作爲閾值 (1<=i<=)(1<= i <= 樣例個數),計算當前預測的前 ii 爲正例的查全率和查準率。然後以查全率爲橫座標,查準率爲縱座標作圖,就得到了我們的查準率-查全率曲線: P-R曲線

舉例說明:以下摘自 多標籤圖像分類任務的評價方法-mAP

在一個識別圖片是否是車,這樣一個二分類任務中,我們訓練好了一個模型。假設測試樣例有20個,用訓練好模型測試可以得到如下測試結果: 其中 id (序號),confidence score (置信度、得分) 和 ground truth label (類別標籤)

接下來對 confidence score 排序,得到:

然後我們開始計算P-R曲線值,將排序後的樣例,從 i=0i = 0i=20i = 20 遍歷,每次將第 ii 個樣例的 confidence scores 做爲閾值,前 ii 個預測爲正例時,計算對應的 Precision 和 Recall。

例如,當 i=1i = 1 時,預測了一個做爲正例,其餘的都預測爲反例,此時的閾值爲 0.91。 此時的 TP=1 就是指第4張,FP=0FN=5 爲第2、9、16、7、20 的圖片,TN=15爲13,19,6,1,18,5,15,10,17,12,14,8,11,3。Precision = TP/(TP+FP) = 1/(1+0) = 0Recall = TP/(TP+FN) = 1/(1+5)=1/6 。接着計算當 i=2i = 2 時,以此類推…

爲了便於理解,我們再講一個例子當 i=5i = 5 時,表示我們選了前5個預測結果認爲是正例(對應圓圈中的TPFP),此時閾值爲0.45,我們得到了 top-5 的結果如下:

在這個例子中,TP 就是指第4和第2張圖片,FP 就是指第13,19,6張圖片。方框內圓圈外的元素(FNTN)是相對於方框內的元素而言,在這個例子中,是指 confidence score 小於當前閾值的元素,即:

3. ROC 與 AUC

ROC 全稱是“受試者工作特徵”(Receiver Operating Characteristic)。ROC 曲線的面積就是 AUC(Area Under the Curve)。AUC用於衡量“二分類問題”機器學習算法性能(泛化能力)。

思想:和計算 P-R 曲線方法基本一致,只是這裏計算的是 真正率(True Positive rate) 和 假正率(False Positive rate),以 FPR 爲橫軸,TPR 爲縱軸,繪製的曲線就是 ROC 曲線,ROC 曲線下的面積,即爲 AUC

TPR=TPTP+FN (真正率)TPR = \frac{TP}{TP + FN}

FPR=FPFP+TN (假正率)FPR = \frac{FP}{FP + TN}

4. mAP

接下來說說 AP 的計算,此處參考的是 PASCAL VOC CHALLENGE 的計算方法。首先設定一組閾值,[0, 0.1, 0.2, …, 1]。然後對於 Recall 大於每一個閾值(比如 Recall > 0.3),我們都會得到一個對應的最大 Precision。這樣,我們就計算出了11個 Precision。AP 即爲這11個 Precision 的平均值。這種方法英文叫做 11-point interpolated average precision
相應的 Precision-Recall 曲線(這條曲線是單調遞減的)如下:

AP 衡量的是學出來的模型在每個類別上的好壞,mAP 衡量的是學出的模型在所有類別上的好壞,得到 AP 後 mAP 的計算就變得很簡單了,就是取所有 AP 的平均值。

5. 代碼簡單實現

% Creation          :  25-Apr-2018 16:01
% Last Reversion    :  25-Apr-2018 16:25
% Author            :  Lingyong Smile {smilelingyong@163.com}
% File Type         :  Matlab
% -----------------------------------------------------------
% mAP_learning()
% This script is used to learning precesion, recall, RP curve, 
% ROC and AUC curve, and mAP.
% ------------------------------------------------------------
% Copyright (c) 2018, Lingyong Smile

%% Initialization
clc;
clear;
close all;
dbstop if error;

%% Load data
data = ...
[[1:20]', ...
[0;1;0;1;0;0;1;0;1;0;0;0;0;0;0;1;0;0;0;1], ...
[0.23; 0.76; 0.01; 0.91; 0.13; 0.45; 0.12; 0.03; 0.38; 0.11; 0.03; 0.09; 0.65; 0.07; 0.12; 0.24; 0.1; 0.23; 0.46; 0.08]];

%% Plot sorted data
[~, sort_idx] = sort(data(:,3), 'descend');
data = data(sort_idx, :);   % descend sort data of confident
figure(1);
subplot(2, 2, 1);
hold on;
% plot(data(:, 1), data(:, 3), '*r'); 
x = [1:length(data(:, 1))+1];
y = [data(:, 3); NaN];
c = y;
patch(x, y, c, 'EdgeColor','interp','Marker','o','MarkerFaceColor','flat'); % which is equals to the comment line
title('data descend sort');
xlabel('id');
ylabel('sorce');
set(gca, 'xtick', [1:size(data, 1)], 'ytick', [0:0.1:1]);  % set grid properties
grid on;

%% Calculate Precision and Recall, False positive rate and True positive rate
for i = 1:size(data, 1)
    TP = sum(data(1:i, 2) == 1);
    FP = sum(data(1:i, 2) == 0);
    FN = sum(data(i+1:end, 2) == 1);
    TN = sum(data(i+1:end, 2) == 0);
    Precision(i) = TP / (TP + FP);
    Recall(i) = TP / (TP + FN);
    TPR(i) = TP / (TP + FN);
    FPR(i) = FP / (FP + TN);
end

%% Plot P-R curve
subplot(2, 2, 2);
plot(Recall, Precision, '-r');
hold on;
plot(Recall, Precision, '.r', 'MarkerSize', 10, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r'); % plot point
set(gca,'xtick',[0:0.1:1], 'ytick', [0:0.1:1]); 
grid on;
axis([0, 1, 0, 1]);
title('2-class P-R curve');
xlabel('Recall');
ylabel('Precision');

%% Plot mAP
ap = [];
thresh = 0:0.1:1;
for t = thresh
    p = max(Precision(Recall >= t));
    if isempty(p)
        p = 0;
    end
    ap(end+1) = p;
end
mAP = sum(ap) / 11;

subplot(2, 2, 4);
hold on;
plot(thresh, ap, '-r');
plot(thresh, ap, '.r', 'MarkerSize', 10, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r'); % plot point
patch('Faces', 1:length(thresh)+2, 'Vertices', [[0, thresh, 1]', [0, ap, 0]'], 'FaceColor','magenta','FaceAlpha',.3);
text(0.2, 0.2, 'mAP', 'FontSize', 12);  % text the mAP
set(gca,'xtick',[0:0.1:1], 'ytick', [0:0.1:1]); 
grid on;
axis([0, 1, 0, 1]);
title(sprintf('2-class P-R curve, mAP=%.4f', mAP));
xlabel('Recall');
ylabel('Precision');

%% Plot ROC and AUC
ap = [];
thresh = [0:1/length(FPR):1];
for t = thresh
    p = max(TPR(FPR <= t));
    if isempty(p)
        p = 0;
    end
    ap(end+1) = p;
end
AUC = sum(ap) / length(thresh);

subplot(2, 2, 3);
hold on;
plot(FPR, TPR, '-.b');
plot(FPR, TPR, '.r', 'MarkerSize', 10, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r'); % plot point
% patch([0, FPR, 1], [0, TPR, 0], 'r'); % which is simple fill red color 
patch('Faces', 1:length(FPR)+2, 'Vertices', [[0, FPR, 1]', [0, TPR, 0]'], 'FaceColor','red','FaceAlpha',.3);
text(0.1, 0.9, 'ROC curve', 'FontSize', 12);  % text the ROC
text(0.6, 0.4, 'AUC area', 'FontSize', 12);  % text the AUC
set(gca,'xtick',[0:0.1:1], 'ytick', [0:0.1:1]); 
grid on;
axis([0, 1, 0, 1]);
title(sprintf('ROC and AUC, AUC=%.4f', AUC));
xlabel('FPR(Ture Positive Rate)');
ylabel('TPR(False Posetiv Rate)');

結果圖:

6. Reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章