selectThreshold.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. bestEpsilon = 0;
  9. bestF1 = 0;
  10. stepsize = (max(pval) - min(pval)) / 1000;
  11. for epsilon = min(pval):stepsize:max(pval)
  12. cvP = pval < epsilon;
  13. tp = sum(cvP & yval);
  14. fp = sum(cvP & (yval == 0));
  15. fn = sum((cvP == 0) & yval);
  16. prec = tp / (tp + fp);
  17. rec = tp / (tp + fn);
  18. F1 = 2 * prec * rec / (prec + rec);
  19. % ====================== YOUR CODE HERE ======================
  20. % Instructions: Compute the F1 score of choosing epsilon as the
  21. % threshold and place the value in F1. The code at the
  22. % end of the loop will compare the F1 score for this
  23. % choice of epsilon and set it to be the best epsilon if
  24. % it is better than the current choice of epsilon.
  25. %
  26. % Note: You can use predictions = (pval < epsilon) to get a binary vector
  27. % of 0's and 1's of the outlier predictions
  28. % =============================================================
  29. if F1 > bestF1
  30. bestF1 = F1;
  31. bestEpsilon = epsilon;
  32. end
  33. end
  34. end