Question

I have a simple question and I'am not very familiar with matlab. so code would be very helpfull ;). I do have a KNN classifier which I want to evaluate via crossvalidation. My code looks like the following:

load ds

train_data= trainData';
train_target=trainLabels;
Num=size(3,3);
Smooth=0.2;

nfold=10


indices = crossvalind('Kfold',train_target,10);

for i = 1:nfold
    test = (indices == i); train = ~test;
    [Prior,PriorN,Cond,CondN]=KNNtr(train_data(train,:),train_target(train,:),Num,Smooth);
    [HammingLoss,RankingLoss,OneError,Coverage,Average_Precision,Outputs,Pre_Labels] = KNNte(train_data(train,:),train_target(train,:),train_data(test,:),train_target(test,:),Num,Prior,PriorN,Cond,CondN);

end

My input data is for the labels 10000*1 and for the training_data 128*10000. Now, when I run the program it results in 1000*1 Pre_Labels or the other outputs as well. I guess this is because I only have 1 fold displayed. All I want is to have all outputs of all folds, in an ordered structure, displayed. How do I have to change my code to achieve this?

Thank you very much!! It's a great help

Was it helpful?

Solution

Maybe values in PreLabel are getting overwritted again and again because you have not defined it to be an array. Define PreLabel to be array like PreLabel(i) so that it can store values for different folds.Similarly if you require values for other variables for every fold define them to be an array as well

for i = 1:nfold
test = (indices == i); train = ~test;
[Prior,PriorN,Cond,CondN]=KNNtr(train_data(train,:),train_target(train,:),Num,Smooth);
[HammingLoss(i),RankingLoss(i),OneError(i),Coverage(i),Average_Precision(i),Outputs(i),Pre_Labels(1)] = KNNte(train_data(train,:),train_target(train,:),train_data(test,:),train_target(test,:),Num,Prior,PriorN,Cond,CondN);
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top