-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaiveBayesTrain.m
More file actions
42 lines (35 loc) · 1.35 KB
/
Copy pathNaiveBayesTrain.m
File metadata and controls
42 lines (35 loc) · 1.35 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
function [p_url_dado_M, p_url_dado_B, P_M, P_B, non_zero_features, stats] = ...
NaiveBayesTrain(X_train, y_train, binary_features,numeric_features)
%[p_url_dado_M, p_url_dado_B, P_M, P_B, non_zero_features, stats] = ...
%NaiveBayesTrain(X_train, y_train, binary_features,numeric_features)
mode = 'Mixed';
if nargin == 3
numeric_features = [];
mode = 'None';
end
X_M = X_train(y_train == 'malign', :);
X_B = X_train(y_train == 'benign', :);
numM = size(X_M, 1);
numB = size(X_B, 1);
total = size(X_train, 1);
P_M = numM / total;
P_B = numB / total;
% Binary features probabilities
non_zero_features = find(sum(X_train(:, binary_features)) ~= 0);
ocorrenciaM = sum(X_M(:, binary_features));
ocorrenciaB = sum(X_B(:, binary_features));
total_Malign = sum(X_M(:, binary_features), 'all');
total_Benign = sum(X_B(:, binary_features), 'all');
p_url_dado_M = (ocorrenciaM + 1) / (total_Malign + length(binary_features));
p_url_dado_B = (ocorrenciaB + 1) / (total_Benign + length(binary_features));
% Gaussian features
if ~strcmp(mode,'None')
meanM = mean(X_M(:, numeric_features), 1);
stdM = std(X_M(:, numeric_features), 1);
meanB = mean(X_B(:, numeric_features), 1);
stdB = std(X_B(:, numeric_features), 1);
stats = [meanM; meanB; stdM; stdB];
else
stats = [];
end
end