Search
• Real Time Signals India

# Naive-Bayes Algorithm In Machine Learning

Updated: Jan 15, 2018

It is a classification technique based on Bayes' Theorem with an assumption of independence among predictors. In simple terms, a Naive Bayes classifier assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature. # MATLAB CODE FOR Naive-Bayes Algorithm

% MATLAB CODE FOR Naive-Bayes Algorithm

% Predict the gender for given height,weight and footsize

% AUTHOR : Real Time Signals

% Column 1 = Height ,column2 =weight, column3= Foot_size column4 = male/female

% 1 = male 0 = female

% P(person is male?person's data) = P(person's data?person is male)? P(person is male)

% -----------------------------------------------------------------

% P(person's data)

% P(person is female?person's data) = P(person's data?person is female)? P(person is female)

% -----------------------------------------------------------------

% P(person's data)

% posterior (male)= P(male)p(height?male)p(weight?male)p(foot size?male)

% ---------------------------------------------------

% marginal probability

% posterior (female)= P(female)p(height?female)p(weight?female)p(foot size?female)

% ---------------------------------------------------

% marginal probability

% Marginal Probability is not required to be calculated as it is same for both

% Assume height and weight are independent of each other

% Find P(1),P(0)

% Find P(H/1),P(W/1),P(F/1)

% Find P(H/0),P(W/0),P(F/0)

% Find the mean and variance

% Fit the Guassian Curve

% Check for an input value

% Find posterior(male) and posterior(female)

% Make a decision

filename = 'C:\Users\user\Desktop\RTS\Naive-Bayes\naive_bayes_simple_test_1.xlsx'; % load the data into the file

x= xlsread(filename); % into the variable x

N = length(x(:,4)); % No of elements in the given data set

N_1 = length(find(x(:,4) == 1)); % No of males in the given data set

N_0 = length(find(x(:,4) == 0)); % No of females in the given data set

P_1 = N_1/N; % Probability(1)

P_0 = N_0/N; % Probability(0)

% (height?male)

h_1_mean = mean(x(x(:,4) == 1 , 1)); % Find the mean height/male

h_1_variance = (((norm(x(x(:,4) == 1 , 1)))^2)/N_1) - h_1_mean^2; % Find the variance height/male

% (weight?male)

w_1_mean = mean(x(x(:,4) == 1 , 2)); % Find the mean height/male

w_1_variance = (((norm(x(x(:,4) == 1 , 2)))^2)/N_1) - w_1_mean^2; % Find the variance height/male

% (foot_size?male)

f_1_mean = mean(x(x(:,4) == 1 , 3)); % Find the mean height/male

f_1_variance = (((norm(x(x(:,4) == 1 , 3)))^2)/N_1) - f_1_mean^2; % Find the variance height/male

% (height?female)

h_0_mean = mean(x(x(:,4) == 0 , 1)); % Find the mean height/female

h_0_variance = (((norm(x(x(:,4) == 0 , 1)))^2)/N_0) - h_0_mean^2; % Find the variance height/female

% (weight?female)

w_0_mean = mean(x(x(:,4) == 0 , 2)); % Find the mean height/female

w_0_variance = (((norm(x(x(:,4) == 0 , 2)))^2)/N_0) - w_0_mean^2; % Find the variance height/female

% (foot_size?female)

f_0_mean = mean(x(x(:,4) == 0 , 3)); % Find the mean height/female

f_0_variance = (((norm(x(x(:,4) == 0 , 3)))^2)/N_0) - f_0_mean^2; % Find the variance height/female

% Test data

test_h =5.5; % Input for height

test_w =150; % Input for weight

test_f =8; % Input for foot size

% Find the probabilities

% Fit Guassian Distributions

% Find probabilities for male

P_H_1 = sqrt((1/(2*pi* h_1_variance))) * exp(-(test_h - h_1_mean)^2/(2* h_1_variance)); % for height

P_W_1 = sqrt((1/(2*pi* w_1_variance))) * exp(-(test_w - w_1_mean)^2/(2* w_1_variance)); % for weight

P_F_1 = sqrt((1/(2*pi* f_1_variance))) * exp(-(test_f - f_1_mean)^2/(2* f_1_variance)); % for foot size

% Find probabilities for male

P_H_0 = sqrt((1/(2*pi* h_0_variance))) * exp(-(test_h - h_0_mean)^2/(2* h_0_variance)); % for height

P_W_0 = sqrt((1/(2*pi* w_0_variance))) * exp(-(test_w - w_0_mean)^2/(2* w_0_variance)); % for weight

P_F_0 = sqrt((1/(2*pi* f_0_variance))) * exp(-(test_f - f_0_mean)^2/(2* f_0_variance)); % for foot size

% Find posterior probabilities

m_p = P_1 * P_H_1 * P_W_1* P_F_1;

f_p = P_0 * P_H_0 * P_W_0* P_F_0;

if(m_p > f_p)

disp('male');

else

disp('female');

end

% Plotting

x(x(:,4) == 1,1);

x(x(:,4) == 0,1);

figure(1);

scatter(x(x(:,4) == 1,1),x(x(:,4) == 1,2),'b');

hold on;

scatter(x(x(:,4) == 0,1),x(x(:,4) == 0,2),'r');

hold on;

scatter(test_h,test_w,'g');

figure(2);

scatter(x(x(:,4) == 1,1),x(x(:,4) == 1,3),'b');

hold on;

scatter(x(x(:,4) == 0,1),x(x(:,4) == 0,3),'r');

hold on;

scatter(test_h,test_f,'g');

Machine Learning Training Data 