Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How can I solve this error ... Error using * Inner matrix dimensions must agree. Error in Naivayes (line 74) 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);

1 Ansicht (letzte 30 Tage)
I got Error Error using * Inner matrix dimensions must agree.
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 strcmp ( yy{j} , yu{i} ) %(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
  2 Kommentare
honi heni
honi heni am 8 Dez. 2016
I got error in the code, How can i rectify this.Thanks
Error using * Inner matrix dimensions must agree. Error in Naivayes (line 74) 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);

Antworten (1)

Chaya N
Chaya N am 8 Dez. 2016
Bearbeitet: Chaya N am 8 Dez. 2016
Please check the values of the variables A1, A2, A3 and A4.
These should be scalar and greater than 1, otherwise they generate 0 or negative indices in line 74, which cannot be accessed.
Also, if any (or all) of these variables are vectors, then each of the multiplicands in the right hand side of line 74 should be either the same size or scalar. In this case, you would need to use element-wise multiplication. Replace the ' * ' operands by ' .* '

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by