-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussianNaiveBayesClassifier.m
51 lines (40 loc) · 1.18 KB
/
gaussianNaiveBayesClassifier.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
function [preds] = gaussianNaiveBayesClassifier(X_train, X_test, y_train, y_test, K)
%GAUSSIANNAIVEBAYESCLASSIFIER
%% INITIALIZATIONS
[N,D] = size(X_train);
Mus = zeros(K,D);
varSqs = zeros(K,D);
%% TRAIN
% Calcultaing priors
fprintf("calculating prior probabilities\n");
priors = [];
for k=1:K
priors = [priors; length(y_train(y_train == k))/N];
end
% calculating means and covs
fprintf("calculating means and covs\n");
for k=1:K
tmp = X_train(find(y_train==k),:); % all the data of class k
for d=1:D
Mus(k,d) = sum(tmp(:,d))/length(y_train(y_train == k)); %mean(tmp(:,d))
varSqs(k,d) = (sum((tmp(:,d)-Mus(k,d)).^2) / length(y_train(y_train == k))); %var(tmp(:,d))
end
end
Vars = varSqs.^(1/2);
%% TEST
%calculating test probabilities
fprintf("calculating test probabilities\n");
probs = [];
for i=1:length(y_test)
samp_probs = [];
for k=1:K
classPred = priors(k);
for d=1:D
classPred = classPred * normpdf(X_test(i,d),Mus(k,d),Vars(k,d));
end
samp_probs = [samp_probs classPred];
end
probs = [probs; samp_probs];
end
[~,preds] = max(probs,[],2);
end