csci5521/assignments/hwk02/Classify.m
2023-10-21 23:32:00 -05:00

31 lines
No EOL
922 B
Matlab

% implements Classify, return the predicted class for each row (we'll call each row x) in data
% by computing the posterior probability that x is in class 1 vs. class 2 then
% these posterior probabilities are compared using the log odds.
function [predictions] = Classify(data, m1, m2, S1, S2, pc1, pc2)
% calculate P(x|C) * P(C) for both classes
pxC1 = mvnpdf(data, m1, S1);
pxC2 = mvnpdf(data, m2, S2);
g1 = log(pxC1 * pc1);
g2 = log(pxC2 * pc2);
% TODO: calculate log odds, if > 0 then data(i) belongs to class c1, else, c2
log_odds = g1 - g2;
% for i = 1:length(data)
% if g1 > g2
% predictions(i) = 1;
% else
% predictions(i) = 2;
% end
% end
% TODO: get predictions from log odds calculation
[num_rows, ~] = size(data);
predictions = zeros(num_rows,1);
for i = 1:num_rows
predictions(i) = log_odds(i) > 0;
end
end % Function end