How to compute the average of several runs of a function?

조회 수: 3 (최근 30일)
phdcomputer Eng
phdcomputer Eng 2019년 12월 10일
댓글: phdcomputer Eng 2019년 12월 11일
I wrote these codes,I loaded the dataset(colon attached) and I randomly selected a part of the data for test and a part for train the classifiers and computed the accuracies.
I want to define a for loop to run these lines 4 times and save the results of the runs in an array and then compute the sum of the array and calculate the average of the 4 runs.I don't know how to add the elements of an array and save it to divide by 4 for average.I'm trying to calculate the average of accuracies.
I want to compute the average for Arforest, ADT , Ask seprately.
the classificationa function is defined for apply the 3 classifiers,and allaccuracydata function is defined for computing the accuracy of each classifier.
I added a for loop and then used sum for add the elements of each array(Arforest,ADT,Ask) and divided the sum by 4 to obtain average. Are these codes right for computing the true average of 4 runs of the program?
I'll be very gratefull to have your opinions. Thanks
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=4;
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
RFf=sum(Arforest);
averf=RFf/it;
DTf=sum(ADT);
avedt=DTf/it;
SKf=sum(Ask);
avesk=SKf/it;
  댓글 수: 2
phdcomputer Eng
phdcomputer Eng 2019년 12월 11일
@Jeremy Marcyoniak Thanks, by using mean after the for loop the result is better and more logical than the codes I added in the question for computing averages.
should I use counter k for the line:
[rforest(k), DT (k), sk(k) ] = classificationa(xtest,xtrain,ytrain);
[Arforest, ADT , Ask] = allaccuracydata(rforest(k), DT (k), sk(k) , ytest);
I didn't use counter(k) for them in the codes but if I use counter, Matlab shows error about dimensions.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by