Path: blob/master/Week 9/Programming Assignment - 8/ex8/selectThreshold.m
616 views
function [bestEpsilon bestF1] = selectThreshold(yval, pval)1%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting2%outliers3% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best4% threshold to use for selecting outliers based on the results from a5% validation set (pval) and the ground truth (yval).6%78bestEpsilon = 0;9bestF1 = 0;10F1 = 0;1112stepsize = (max(pval) - min(pval)) / 1000;13for epsilon = min(pval):stepsize:max(pval)1415% ====================== YOUR CODE HERE ======================16% Instructions: Compute the F1 score of choosing epsilon as the17% threshold and place the value in F1. The code at the18% end of the loop will compare the F1 score for this19% choice of epsilon and set it to be the best epsilon if20% it is better than the current choice of epsilon.21%22% Note: You can use predictions = (pval < epsilon) to get a binary vector23% of 0's and 1's of the outlier predictions242526cvPredictions = (pval < epsilon);2728tp = sum((cvPredictions == 1) & (yval == 1));29fp = sum((cvPredictions == 1) & (yval == 0));30fn = sum((cvPredictions == 0) & (yval == 1));3132prec = tp /(tp + fp);33rec = tp /(tp + fn);3435F1 = 2 * prec * rec /(prec + rec);36% =============================================================3738if F1 > bestF139bestF1 = F1;40bestEpsilon = epsilon;41end42end4344end454647