How to compute the average of several runs of a function?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!