-
Notifications
You must be signed in to change notification settings - Fork 0
/
NB_Train.m
36 lines (31 loc) · 1.1 KB
/
NB_Train.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Naive Bayes classifier training
%
% This function computes the parameters of a Naive Bayes classifier
% for the given training dataset
%
% Inputs:
%
% training_data - The training data set, one sample PER ROW
% training_labels - Labels for the training dataset
% K - Number of classes
%
% Return values:
%
% NB_probs - A kxD array whose rows are the probabilities
% for input features being = 1 for each class
% NB_ais - A kx1 array with p(L=i)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [NB_probs, NB_ais]=NB_Train(training_data, training_labels, K)
%change training data to binary;
training_data = training_data > 0;
for i=1:K
label_size(i,:) = size(training_labels(training_labels == i-1,:),1);
class_data{i} = training_data(training_labels == i-1,:);
end;
NB_ais = label_size ./ sum(label_size);
for j=1:K
NB_probs(j,:) = sum(class_data{j}) ./ size(class_data{j},1); %
end;
% set 0s in prob to be 1e-9
NB_probs = (NB_probs == 0)*1e-9 + NB_probs;