Matlab: cvpartition()函数实现交叉验证

下面的代码是将一组数据进行10折交叉划分,并将划分好的训练集合测试集存放在一个文件夹下面:

% Note: this code should be run from orca/src/code-examples
clear param;
if (exist ('OCTAVE_VERSION', 'builtin') > 0)
    pkg load statistics
end

% Load data
ERAData = csvread('../../exampledata/ERA.csv');

% Check the first 20 rows
ERAData(1:20,:);

% Extract targets
targets = ERAData(:,end);

% Generate k fold partitions
k=10;
CVO = cvpartition(targets,'KFold',k);

% Prepare filesystem
nameDataset = 'era';
rootDir = fullfile('..', '..', 'exampledata', '10-fold', nameDataset);
mkdir(rootDir);
rootDir = fullfile(rootDir,'matlab');
mkdir(rootDir);

% For each fold
for ff = 1:k
    if (exist ('OCTAVE_VERSION', 'builtin') > 0)
        trIdx = training(CVO,ff);
        teIdx = test(CVO,ff);
    else
        trIdx = CVO.training(ff);
        teIdx = CVO.test(ff);
    end
    dlmwrite(fullfile(rootDir,sprintf('train-%s.%d',nameDataset,ff-1)),ERAData(trIdx,:),' ');
    dlmwrite(fullfile(rootDir,sprintf('test-%s.%d',nameDataset,ff-1)),ERAData(teIdx,:),' ');
end

关于 cvpartition()函数的使用说明请参考:

https://ww2.mathworks.cn/help/stats/cvpartition.html

你可能感兴趣的:(Matlab学习)