Created
February 11, 2021 12:15
-
-
Save AdroitAnandAI/123ec6ce199b7945d5ffb9bde244be2c to your computer and use it in GitHub Desktop.
Find anomalies from data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [mu sigma2] = estimateGaussian(X) | |
% To estimate parameters of Gaussian distribution using data | |
% Useful variables | |
[m, n] = size(X); | |
% You should return these values correctly | |
mu = zeros(n, 1); | |
sigma2 = zeros(n, 1); | |
% compute mean and variance | |
mu = mean(X); | |
sigma2 = sum((X - repmat(mu, m, 1)).^2)/m; | |
end | |
function [bestEpsilon bestF1] = selectThreshold(yval, pval) | |
% Find the best threshold (epsilon) to use for selecting outliers | |
bestEpsilon = 0; | |
bestF1 = 0; | |
F1 = 0; | |
stepsize = (max(pval) - min(pval)) / 1000; | |
for epsilon = min(pval):stepsize:max(pval) | |
% Compute F1 score of choosing epsilon threshold. | |
% This code 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. | |
predictions = (pval < epsilon); | |
tp = sum(predictions.*yval); % detects 1 1 (prediction, actual) | |
fp = sum(predictions > yval); % detects 1 0 (prediction, actual) | |
fn = sum(predictions < yval); % detects 0 1 (prediction, actual) | |
prec = tp / (tp + fp); | |
rec = tp / (tp + fn); | |
F1 = 2 * prec * rec / (prec + rec); | |
if F1 > bestF1 | |
bestF1 = F1; | |
bestEpsilon = epsilon; | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment