forked from oweisse/dpav4-contest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomGaussianClassifier.m
96 lines (81 loc) · 3.79 KB
/
CustomGaussianClassifier.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
% Author: Ofir Weisse, mail: oweisse (at) umich.edu, www.ofirweisse.com
%
% MIT License
%
% Copyright (c) 2016 oweisse
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
%Author: Ofir Weisse, www.ofirweisse.com, [email protected]
%based on Matlab implementation of GaussianClassifier
classdef CustomGaussianClassifier
properties
NDims
ClassLevels
NClasses
Means
Sigmas
Priors
end
methods
function obj = CustomGaussianClassifier( ndims, ...
class_levels, ...
means, ...
sigmas, ...
priors )
obj.NDims = ndims;
obj.ClassLevels = class_levels;
obj.NClasses = length( class_levels );
obj.Means = means;
obj.Sigmas = sigmas;
obj.Priors = priors;
end
function postrior_porabilities = Postrior( obj, test )
nTest = size( test, 1 );
logCondPDF = NaN(nTest, obj.NClasses);
debugVotes = zeros( obj.NClasses, obj.NDims );
for class_idx = 1:obj.NClasses
logPdf = zeros(nTest,1);
class_params = [ obj.Means( class_idx, : ); obj.Sigmas( class_idx, : ) ];
m1 = true( 1, obj.NDims );
templogPdf = bsxfun(@plus, -0.5* (bsxfun(@rdivide,...
bsxfun(@minus,test(:,m1),class_params(1,:)),class_params(2,:))) .^2,...
-log(class_params(2,:))) -0.5 *log(2*pi);
debugVotes( class_idx, : ) = templogPdf;
logPdf = logPdf + sum(templogPdf,2);
logCondPDF(:,class_idx)= logPdf;
end
log_condPdf =bsxfun(@plus,logCondPDF, log(obj.Priors));
[maxll, cidx] = max(log_condPdf,[],2);
postP = exp(bsxfun(@minus, log_condPdf, maxll));
%density(i) is \sum_j \alpha_j P(x_i| \theta_j)/ exp(maxll(i))
density = nansum(postP,2); %ignore the empty classes
%normalize posteriors
postP = bsxfun(@rdivide, postP, density);
postrior_porabilities = postP;
end
function [ predictions, postriors ] = predict( obj, test )
postrior_porabilities = obj.Postrior( test );
[~, class_ids ] = max( postrior_porabilities, [], 2 );
predictions = obj.ClassLevels( class_ids );
if nargout == 2
postriors = postrior_porabilities;
end
end
end
end