-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgmm_dd.m
executable file
·68 lines (56 loc) · 1.88 KB
/
gmm_dd.m
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
62
63
64
65
66
67
68
%GMM_DD Gaussian Mixture Model classifier
%
% W = GMM_DD(A,FRACREJ, N)
%
% INPUT
% A Dataset
% FRACREJ Fraction of target objects rejected (default = 0.05)
% N Number of Gaussians (default = 1)
%
%
function W = gmm_dd(a, fracrej, n)
% Take care of empty/not-defined arguments:
if nargin < 3 || isempty(n), n = 1; end
if nargin < 2 || isempty(fracrej), fracrej = 0.05; end
if nargin < 1 || isempty(a)
% When no inputs are given, we are expected to return an empty
% mapping:
W = prmapping(mfilename,{fracrej, n});
% And give a suitable name:
W = setname(W,sprintf('Gaussian Mixture Model n:%d', n));
return
end
if ~ismapping(fracrej) %training
rng('default')
a = +target_class(a); % only use the target class
[m,d] = size(a);
W.n = n;
% Fit a Gaussian mixture distribution to data
W.w = fitgmdist(a, n, 'Options', statset('Display','off','MaxIter',1000,'TolFun',1e-6), 'RegularizationValue', 0.01);
% computes outlier scores for training set
if(W.n > 1)
W.scores = -min(mahal(W.w, a)')';
else
W.scores = -mahal(W.w, +a);
end
% obtain the threshold
W.out = -W.scores;
W.threshold = dd_threshold(W.scores, fracrej);
W = prmapping(mfilename,'trained',W,str2mat('target','outlier'),d,2);
W = setname(W,sprintf('n:%d ', n));
else %testing
% Unpack the mapping and dataset:
W = getdata(fracrej);
[m,d] = size(a);
if(W.n > 1)
testScores = -min(mahal(W.w, +a)')';
else
testScores = -mahal(W.w, +a);
end
% outlier scores for testing data
out = [testScores repmat(W.threshold,[m,1])];
% Fill in the data, keeping all other fields in the dataset intact:
W = setdat(a, out, fracrej);
W = setfeatdom(W,{[-inf 0;-inf 0] [-inf 0;-inf 0]});
end
return