Select threshold
parent
4bc9a2b246
commit
87457bb7b4
@ -1,46 +1,44 @@
|
|||||||
function [bestEpsilon bestF1] = selectThreshold(yval, pval)
|
function [bestEpsilon bestF1] = selectThreshold(yval, pval)
|
||||||
%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting
|
%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting
|
||||||
%outliers
|
%outliers
|
||||||
% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best
|
% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best
|
||||||
% threshold to use for selecting outliers based on the results from a
|
% threshold to use for selecting outliers based on the results from a
|
||||||
% validation set (pval) and the ground truth (yval).
|
% validation set (pval) and the ground truth (yval).
|
||||||
%
|
%
|
||||||
|
|
||||||
bestEpsilon = 0;
|
bestEpsilon = 0;
|
||||||
bestF1 = 0;
|
bestF1 = 0;
|
||||||
F1 = 0;
|
F1 = 0;
|
||||||
|
|
||||||
stepsize = (max(pval) - min(pval)) / 1000;
|
stepsize = (max(pval) - min(pval)) / 1000;
|
||||||
for epsilon = min(pval):stepsize:max(pval)
|
for epsilon = min(pval):stepsize:max(pval)
|
||||||
|
|
||||||
% ====================== YOUR CODE HERE ======================
|
% ====================== YOUR CODE HERE ======================
|
||||||
% Instructions: Compute the F1 score of choosing epsilon as the
|
% Instructions: Compute the F1 score of choosing epsilon as the
|
||||||
% threshold and place the value in F1. The code at the
|
% threshold and place the value in F1. The code at the
|
||||||
% end of the loop will compare the F1 score for this
|
% end of the loop will compare the F1 score for this
|
||||||
% choice of epsilon and set it to be the best epsilon if
|
% choice of epsilon and set it to be the best epsilon if
|
||||||
% it is better than the current choice of epsilon.
|
% it is better than the current choice of epsilon.
|
||||||
%
|
%
|
||||||
% Note: You can use predictions = (pval < epsilon) to get a binary vector
|
% Note: You can use predictions = (pval < epsilon) to get a binary vector
|
||||||
% of 0's and 1's of the outlier predictions
|
% of 0's and 1's of the outlier predictions
|
||||||
|
|
||||||
|
predictions = (pval < epsilon);
|
||||||
|
|
||||||
|
tp = sum((predictions == 1) & (yval == 1));
|
||||||
|
fp = sum((predictions == 1) & (yval == 0));
|
||||||
|
fn = sum((predictions == 0) & (yval == 1));
|
||||||
|
prec = tp/(tp+fp);
|
||||||
|
rec = tp/(tp+fn);
|
||||||
|
|
||||||
|
F1 = (2*prec*rec)/(prec+rec);
|
||||||
|
|
||||||
|
% =============================================================
|
||||||
|
|
||||||
% =============================================================
|
if F1 > bestF1
|
||||||
|
bestF1 = F1;
|
||||||
if F1 > bestF1
|
bestEpsilon = epsilon;
|
||||||
bestF1 = F1;
|
end
|
||||||
bestEpsilon = epsilon;
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
|
Reference in New Issue