-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectThreshold.m
More file actions
executable file
·61 lines (48 loc) · 1.61 KB
/
selectThreshold.m
File metadata and controls
executable file
·61 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [bestEpsilon bestF1] = selectThreshold(yval, pval)
%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting
%outliers
% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best
% threshold to use for selecting outliers based on the results from a
% validation set (pval) and the ground truth (yval).
%
bestEpsilon = 0;
bestF1 = 0;
F1 = 0;
stepsize = (max(pval) - min(pval)) / 1000;
for epsilon = min(pval):stepsize:max(pval)
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the F1 score of choosing epsilon as the
% threshold and place the value in F1. The code at the
% end of the loop will compare the F1 score for this
% choice of epsilon and set it to be the best epsilon if
% it is better than the current choice of epsilon.
%
% Note: You can use predictions = (pval < epsilon) to get a binary vector
% of 0's and 1's of the outlier predictions
predictions = (pval<epsilon) ;
tp = 0 ;
fp = 0 ;
fn = 0 ;
for i=1:size(yval)
if predictions(i)==1
if yval(i)==1
tp = tp+1 ;
else
fp = fp+1 ;
end
else
if yval(i)==1
fn = fn+1 ;
end
end
end
prec = tp/(tp+fp) ;
rec = tp/(tp+fn) ;
F1 = 2*prec*rec/(prec+rec) ;
% =============================================================
if F1 > bestF1
bestF1 = F1;
bestEpsilon = epsilon;
end
end
end