I get this error Error using == Matrix dimensions must agree. Error in Naivayes (line 21) if (yy{j}==yu{i})
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
honi heni
am 8 Dez. 2016
Bearbeitet: honi heni
am 8 Dez. 2016
Am trying to debug and run this code in matlab , Am totally new to Matlab
fid = fopen('C:\Users\Oni\Documents\MATLAB\data.csv')';
out = textscan(fid,'%s%s%s%s%s','delimiter',',');
fclose(fid);
num_featureswithclass = size(out,2);
tot_rec = size(out{size(out,2)},1) -1;
for i = 1:tot_rec
yy{i} = out{num_featureswithclass}{i+1};
end
for i = 1: num_featureswithclass
xx{i} = out{i};
end
%Calculation of Prior Probability
yu = unique(yy) %unique class label
nc = length(yu) %number of classes
fy = zeros(nc,1);
num_of_rec_for_each_class = zeros(nc,1);
for i=1:nc
for j =1:tot_rec
if (yy{j}==yu{i})
num_of_rec_for_each_class(i) = num_of_rec_for_each_class(i) +1;
end
end
end
%Likelihood Table
prob_table = zeros(num_featureswithclass-1,10,nc);
for col = 1:num_featureswithclass-1
unique_value = unique(xx{col});
rec_unique_value{col} = unique_value;
for i = 2: length(unique_value)
for j = 2:tot_rec+1
if strcmp (xx{col}{j}, unique_value{i}) == 1 && strcmp(xx{num_featureswithclass}{j}, yu{1}) == 1
prob_table(col, i-1,1) = prob_table(col,i-1,1) +1;
end
if strcmp(xx{col}{j}, unique_value{i}) == 1 && strcmp (xx{num_featureswithclass}{j}, yu{2}) == 1
prob_table(col, i-1,2) = prob_table(col,i-1,2) + 1;
end
end
end
end
prob_table(:,:,1) = prob_table(:,:,1)./num_of_rec_for_each_class(1);
prob_table(:,:,2) = prob_table(:,:,2)./num_of_rec_for_each_class(2);
%The matrix "prob_table" used in the above code is a matrix of
%4 * 10 * 2 deimension where "4" is the number of attributes in the
% dataset.The number "10" is the possible number of unique value in
% any attribute.In this example,the maximum number was "3". The
% number "2" refer to the number of classes. If we see the values
% present in theprob_table, the understanding will be further
% enhanced
%
%Predicting for an unlabeled record:
%Now that we have a naive Bayesian classifier in the form of
%tables, we can use them to predict newly arriving unlabeled
%records. The following code snippet describes the prediction
%process in matlab
A = {'Sunny', 'hot', 'high', 'false'};
A1= find(ismember(rec_unique_value{1},A{1}));
A11 = 1;
A2 = find(ismember(rec_unique_value{2},A{2}));
A21=2;
A3 = find(ismember(rec_unique_value{3}, A{3}));
A31 = 3;
A4 = find(ismember(rec_unique_value{4},A{4}));
A41 = 4;
ProbN = prob_table(A11,A1 - 1,1) *prob_table(A21,A2 - 1,1) *prob_table(A31, A3 - 1,1) *prob_table(A41,A4 - 1,1) *fy(1);
ProbP = prob_table(A11,A1 - 1,2) *prob_table(A21,A2 - 1,2) *prob_table(A31, A3 - 1,2) *prob_table(A41,A4 - 1,2) *fy(2);
if ProbN > ProbP
prediction = 'N'
else
prediction = 'P'
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 8 Dez. 2016
Your yy and yu are strings. You will need to use strcmp() to compare them, not ==
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Pattern Recognition and Classification finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!