Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 9/Programming Assignment - 8/ex8/selectThreshold.m
616 views
1
function [bestEpsilon bestF1] = selectThreshold(yval, pval)
2
%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting
3
%outliers
4
% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best
5
% threshold to use for selecting outliers based on the results from a
6
% validation set (pval) and the ground truth (yval).
7
%
8
9
bestEpsilon = 0;
10
bestF1 = 0;
11
F1 = 0;
12
13
stepsize = (max(pval) - min(pval)) / 1000;
14
for epsilon = min(pval):stepsize:max(pval)
15
16
% ====================== YOUR CODE HERE ======================
17
% Instructions: Compute the F1 score of choosing epsilon as the
18
% threshold and place the value in F1. The code at the
19
% end of the loop will compare the F1 score for this
20
% choice of epsilon and set it to be the best epsilon if
21
% it is better than the current choice of epsilon.
22
%
23
% Note: You can use predictions = (pval < epsilon) to get a binary vector
24
% of 0's and 1's of the outlier predictions
25
26
27
cvPredictions = (pval < epsilon);
28
29
tp = sum((cvPredictions == 1) & (yval == 1));
30
fp = sum((cvPredictions == 1) & (yval == 0));
31
fn = sum((cvPredictions == 0) & (yval == 1));
32
33
prec = tp /(tp + fp);
34
rec = tp /(tp + fn);
35
36
F1 = 2 * prec * rec /(prec + rec);
37
% =============================================================
38
39
if F1 > bestF1
40
bestF1 = F1;
41
bestEpsilon = epsilon;
42
end
43
end
44
45
end
46
47