交叉验证分类器在matlab中显示输出

时间:2013-10-30 10:00:56

标签: matlab pattern-recognition cross-validation

我有一个简单的问题,我对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折显示。我想要的是显示所有折叠的所有输出,以有序的结构。我如何更改代码才能实现此目的?

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

1 个答案:

答案 0 :(得分:0)

也许PreLabel中的值会被反复覆盖,因为您尚未将其定义为数组。将PreLabel定义为类似于PreLabel(i)的数组,以便它可以存储不同折叠的值。同样,如果每个折叠需要其他变量的值,则将它们定义为数组

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