-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerativeClassification.m
More file actions
51 lines (40 loc) · 1.33 KB
/
generativeClassification.m
File metadata and controls
51 lines (40 loc) · 1.33 KB
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] = generativeClassification(X_train, X_test, y_train, y_test, K)
%GENERATIVECLASSIFICATIONWITHNAIVEBAYES
%Reference: Bishop, C. M. (2006). Pattern recognition and machine learning - Springer (Chapter 4.2.1)
%Andrew NG - Machine learning course lecture notes part IV
[N,D] = size(X_train);
%% TRAIN
% calculating prior probabilities and means
fprintf("calculating prior probabilities and means\n")
priors = [];
Mus = [];
for k=1:K
priors = [priors; length(y_train(y_train == k))/N];
Mus = [Mus; sum(X_train(find(y_train==k),:))/length(y_train(y_train == k))];
end
% calculating covariance matrix
% CovMat = zeros(D);
% for n=1:N
% tempCov = (X_train(n,:) - Mus(y_train(n)))' * (X_train(n,:) - Mus(y_train(n)));
% CovMat = CovMat + tempCov;
% end
% CovMat = CovMat./N;
fprintf("calculating covariance matrix\n")
covMat = cov(X_train);
% covMat = 1 * eye(D);
covMatInv = pinv(covMat);
% calculating each class coef parameters
fprintf("calculating each class coef parameters\n")
W = (covMatInv * Mus')';
W_0 = [];
for k=1:K
% W = [W: covMatInv*
W_0 = [W_0; -1/2*(Mus(k,:) * covMatInv * Mus(k,:)') + log(priors(k))];
end
%% TEST
fprintf("Testing ...")
res = W * X_test' + W_0;
% preds = softmax(res');
[~, preds] = max(res,[],1);
preds = preds';
end