0

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

loop
  • 17
  • 3

1 Answers1

0

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
Mohit Jain
  • 733
  • 3
  • 9
  • 24
  • Thanks for the reply! I tried that already and I get the following error message:"In an assignment A(I) = B, the number of elements in B and I must be the same. Error in KNNown2 (line 29) [HammingLoss,RankingLoss,OneError,Coverage,Average_Precision,Outputs,Pre_Labels(‌​i)] = MLKNN_test(train_data(train,:),train_target(train,:),train_data(test,:),train_ta‌​rget(test,:),Num,Prior,PriorN,Con " Any suggestions? Do I need to assign an empty matrix in which I store all the data iteravly and if, how do I do it? – loop Oct 30 '13 at 12:36
  • First initialize the variables such as PreLabel=[][](because it is 2D Array) then assign PreLabel(i,:) – Mohit Jain Oct 30 '13 at 13:59
  • ok, I assigned Outputs=zeros(indices,nfold); and then [HammingLoss,RankingLoss,OneError,Coverage,Average_Precision,Outputs(i,:),Pre_Labels] = MLKNN_test(train_data(train,:),train_target(train,:),train_data(test,:),train_target(test,:),Num,Prior,PriorN,Cond,CondN); still don't get the result. Thanks alot again!!! – loop Oct 30 '13 at 17:33
  • try Outputs=[][]; don't initialize it with any thing and see if it works!! – Mohit Jain Oct 30 '13 at 17:45