How to use matlab crossval do the computation for one of the partitions (in k-fold cross validation)

Viewed 317

I'm trying to apply cross validated LDA using matlab cross validation method. To do this I put the crossval() in a loop and in each loop I extract corresponding train and test labels and feature matrix (trFV, tsFV). It's like the example presented in Matlab cvpartition class:

cvp = cvpartition(labelCell,'KFold',kFoldCV)
for i = 1:cvp.NumTestSets
    trFV = f(cvp.training(i), :);
    tsFV = f(cvp.test(i), :);

    % calculate LDA projection matrix:
    [~, W] = LDA(trFV, featureMat(cvp.training(i), end));
    % apply W to both train and test:
    trFVW = trFV * W(:, 1:numel(classes)-1);
    tsFVW = tsFV * W(:, 1:numel(classes)-1);
    fW = [trFVW; tsFVW];
    labels = [labelCell(cvp.training(i)); labelCell(cvp.test(i))];


    Mdl = fitcecoc(fW, labels, 'Coding', 'onevsall',...
        'Learners', learnerTemplate,...
        'ClassNames', classes);
    CVMdl = crossval(Mdl, 'CVPartition', cvp);

    % Other stuff
end

This implementation is very inefficient since I just need the result for one of the folds (not entire folds). I process each fold once in a loop while crossval process whole loops in each loop. Hence in current implementation instead of cvp.NumTestSets times it perform the cross validation cvp.NumTestSets^2 times. I need something like this:

CVMdl = crossval(Mdl, 'CVPartition', cvp, 'compute just for partition i and not all partitions');

Update

The code above have some problem regarding cross validation. However I'm still interested if Matlab built-in LDA (linear discriminant analysis) could be used to reduce dimension.

0 Answers
Related