how to compute the average of several repeats of a program in matlab?

2 次查看(过去 30 天)
I wrote this program to classify the dataset(colon) and compute the accuracy of the classifier, I wanted to repeat the classification 10 times and save the results in an array(Arforest)and then compute the average of these results(averf) but when I test size(Arforest) the size of the result is 1 so any array isn't shaped.
clc;
clear;
close all;
tic
load colon.mat
data=colon; [n,m]=size(data);
rows=(1:n);
test_count=floor((0.2)*n);
sum_ens=0;sum_result=0;
it=10;
for k=1:it
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
test=data(test_rows,:);
train=data(train_rows,:);
xtest=test(:,1:m-1);
ytest=test(:,m);
xtrain=train(:,1:m-1);
ytrain=train(:,m);
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
[Arforest, ADT , Ask] = allaccuracydata(rforest, DT , sk , ytest);
end
averf=mean(Arforest);
avedt=mean(ADT);
avesk=mean(Ask);
Arforest is the result of Random Forest classifier,
when I use the counter (k) an error shows:
it=10;
for k=1:it
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
[Arforest(k), ADT(k) , Ask(k)] = allaccuracydata(rforest(k), DT(k) , sk(k) , ytest);
end
averf=mean(Arforest);
avedt=mean(ADT);
avesk=mean(Ask);
the error is:
Error: File: myFSmethod20.m Line: 157 Column: 19
An array for multiple LHS assignment cannot contain expressions.
I'll be gratefull to have your opinions to obtain true result.Thanks
  2 个评论
Image Analyst
Image Analyst 2019-12-22
Try reading this link first, then fix your post to
  1. attach colon.mat and
  2. include the entire error message (ALL the red text), including the line of code that threw that error
  3. indent your code (control-a, control-i in the MATLAB editor)

请先登录,再进行评论。

采纳的回答

dpb
dpb 2019-12-22
编辑:dpb 2019-12-22
An array for multiple LHS assignment cannot contain expressions.
in
[Arforest(k), ADT(k) , Ask(k)] = allaccuracydata(rforest(k), DT(k) , sk(k) , ytest);
the arguments to allaccuracydata aren't arrays; they're the single-case results from the previous line. Just assign the results:
[Arforest(k) ADT(k) Ask(k)] = allaccuracydata(rforest,DT,sk,ytest);
each loop.
It would be better to preallocate the LHS arrays prior to beginning the loop.
  2 个评论
phdcomputer Eng
phdcomputer Eng 2019-12-22
@ dpb Thank you very much, I used counter k as you adviced and the program run well. and now when I use size(Arforest) the answer is 1 10.
so it means the Arforest(the result of the loop) is an 1x10 size array (has 1 row &10 columns).I'm vary grateful.
phdcomputer Eng
phdcomputer Eng 2019-12-22
编辑:phdcomputer Eng 2019-12-22
@dpb I'll be vary grateful to have your advice for my next question.I want to use the above codes in this way:
clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
%%
%supervised
d=10;
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
%sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
datas6=data(:,indA6(1:d));
data6=[datas6 data(:,m)];
%%data6 classify%%tanimoto
[n,m]=size(data6);
for k=1:it
test=data6(test_rows,:);
train=data6(train_rows,:);
xtest=test(:,1:m-1);
ytest=test(:,m);
xtrain=train(:,1:m-1);
ytrain=train(:,m);
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
[Arforest6(k), ADT6(k) , Ask6(k)] = allaccuracydata(rforest, DT , sk , ytest);
end
averf6=mean(Arforest6);
avedt6=mean(ADT6);
avesk6=mean(Ask6);
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
in this code d is 10 so we use ten features of the data to classify, my question is suppose that we want to obtain the average of Arforest (averf6) once for d=10, once for d=20,for d=30,d=40 and d=50 and save the result of averf6 for each of them in an array(forexample a) to plot it.
how can I save the results (averf6 that is the average of Arforest6) in an array based on changing in d [10 20 30 40 50], I have problem in this part how to define d in these several values and give it to classifier and save the results in an array (a).
I'm vary grateful for your attention. Thanks greatly

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by