in MATLAB, find std with using for loop

15 views (last 30 days)
Eda Çelik
Eda Çelik on 19 Dec 2019
Commented: Eda Çelik on 19 Dec 2019
I write a code for Naive Bayes Classification. But my code is working wrong. In the std calculation part, I want to work for all databases. I wrote it one by one but it is work only my database. I attached my code as pr.m
Help me, please. Where is my mistake? Here is my code: pr.m
%Reading dataset and getting the inputs from user by using input operation
matrix = xlsread("Transfusion.xlsx");
%Find attribute numbers in the dataset
attributesNum = size(matrix,2) - 1 ;
%create an empty matrix for inputs
X = zeros(attributesNum,1);
%İt is a for loop for take inputs from user
for i=1:attributesNum
values = input('Enter Value: ');
X(i,1) = values;
end
%By using this, we create 2 matrices. class 0 and class 1
mask = matrix(:,end) == 0;
c0 = matrix(mask,:);
c1 = matrix(~mask,:);
%By using the input data we will first find the mean of each classes means
%of columns. To do that we are going to use mean() operation. This
%operation takes the mean of each column of each class
m0=mean(c0(:,attributesNum));
% disp(m1);
m1=mean(c1(:,attributesNum));
% disp(m2);
for i=1:attributesNum
for j=1:attributesNum
m(i,j)= mean(c0(:,j));
m(i,j)= mean(c1(:,j));
end
end
zo=zeros(size(c0));
%By using those mean values we are going to find centered data matrix of
%the dataset for each column of each class
meanC0 = m0;
Z0 = c0' - meanC0';
for i=1:attributesNum
for j=1:attributesNum
z0(i,j)= Z0(i,j);
end
end
disp(z0);
meanC1 = m0;
Z1 = c1' - meanC1';
for i=1:attributesNum
for j=1:attributesNum
z1(i,j)= Z1(i,j);
end
end
%At this part we are calculating the standard deviation of each column of
%each classas centered data matrix columns
std11 = (std(Z11,1))^2;
std12 = (std(Z12,1))^2;
std13 = (std(Z13,1))^2;
std14 = (std(Z14,1))^2;
std21 = (std(Z21,1))^2;
std22 = (std(Z22,1))^2;
std23 = (std(Z23,1))^2;
std24 = (std(Z24,1))^2;
%In this part we are calculating the size of both classes and the whole
%matrix. Then after we found those values we are going to find the
%probability of each classes
n1 = size(c0,1);
n2 = size(c1,1);
n = size(matrix,1);
Pc0 = n1/n;
Pc1 = n2/n;
%In this part we compute the probability density function (pdf) values evaluated at the values in xi i=1,2,3,4 for the normal distribution with mean mu and standard deviation sigma.
x1 = X(1,:);
x2 = X(2,:);
x3 = X(3,:);
x4 = X(4,:);
P11 = normpdf(x1,mean11,std11);
P21 = normpdf(x2,mean12,std12);
P31 = normpdf(x3,mean13,std13);
P41 = normpdf(x4,mean14,std14);
P12 = normpdf(x1,mean21,std21);
P22 = normpdf(x2,mean22,std22);
P32 = normpdf(x3,mean23,std23);
P42 = normpdf(x4,mean24,std24);
%Then we are going to multiply all the probabilities of each classes with
%probability density function of the class( For each class seperately).
%Then we take the maximum of the two values that we calculate. Then answer
%is the maximum one. Which one is maximum, it is the class what the user
%input included.
firstClass = Pc0*P11*P21*P31*P41;
secondClass = Pc1*P12*P22*P32*P42;
disp(firstClass);
disp(secondClass);
if firstClass>secondClass
disp("According to input data, He/she did not donate blood in March 2017");
elseif secondClass>firstClass
disp("According to input data, He/she did donate blood in March 2017");
else
disp("no data");
end
  3 Comments

Sign in to comment.

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!