0

我有一个简单的问题,我对 matlab 不是很熟悉。所以代码会很有帮助;)。我确实有一个 KNN 分类器,我想通过交叉验证对其进行评估。我的代码如下所示:

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

我的输入数据是标签 10000*1 和 training_data 128*10000。现在,当我运行程序时,它也会产生 1000*1 Pre_Labels 或其他输出。我想这是因为我只显示了 1 折。我想要的只是以有序结构显示所有折叠的所有输出。我必须如何更改我的代码才能实现这一目标?

非常感谢!!这是一个很大的帮助

4

1 回答 1

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
于 2013-10-30T11:15:03.210 回答