在MATLAB中使用神经网络分类进行10次交叉验证的示例

时间:2016-01-11 14:55:44

标签: matlab machine-learning neural-network classification cross-validation

我正在寻找在神经网络中应用10倍交叉验证的示例。我需要这个问题的链接答案:Example of 10-fold SVM classification in MATLAB

我想对所有3个类进行分类,而在示例中只考虑了两个类。

编辑:这是我为iris示例编写的代码

load fisheriris                              %# load iris dataset

k=10;
cvFolds = crossvalind('Kfold', species, k);   %# get indices of 10-fold CV
net = feedforwardnet(10);


for i = 1:k                                  %# for each fold
    testIdx = (cvFolds == i);                %# get indices of test instances
    trainIdx = ~testIdx;                     %# get indices training instances

    %# train 

    net = train(net,meas(trainIdx,:)',species(trainIdx)');
    %# test 
    outputs = net(meas(trainIdx,:)');
    errors = gsubtract(species(trainIdx)',outputs);
    performance = perform(net,species(trainIdx)',outputs)
    figure, plotconfusion(species(trainIdx)',outputs)
end

错误由matlab提供:

Error using nntraining.setup>setupPerWorker (line 62)
Targets T{1,1} is not numeric or logical.

Error in nntraining.setup (line 43)
    [net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);

Error in network/train (line 335)
[net,data,tr,err] = nntraining.setup(net,net.trainFcn,X,Xi,Ai,T,EW,enableConfigure,isComposite);

Error in Untitled (line 17)
    net = train(net,meas(trainIdx,:)',species(trainIdx)');

1 个答案:

答案 0 :(得分:5)

使用MATLAB的crossval函数要比使用crossvalind手动操作简单得多。由于您只是询问如何从交叉验证中获得测试“得分”,而不是使用它来选择最佳参数,例如隐藏节点的数量,您的代码将如此简单:

load fisheriris;

% // Split up species into 3 binary dummy variables
S = unique(species);
O = [];
for s = 1:numel(S)
    O(:,end+1) = strcmp(species, S{s});
end

% // Crossvalidation
vals = crossval(@(XTRAIN, YTRAIN, XTEST, YTEST)fun(XTRAIN, YTRAIN, XTEST, YTEST), meas, O);

剩下的就是写一个函数fun,它接受​​输入和输出训练和测试集(所有这些都由crossval函数提供给你,所以你不必担心分裂你的数据自己),在训练集上训练神经网络,在测试集上测试它,然后使用您的首选指标输出分数。所以像这样:

function testval = fun(XTRAIN, YTRAIN, XTEST, YTEST)

    net = feedforwardnet(10);
    net = train(net, XTRAIN', YTRAIN');

    yNet = net(XTEST');
    %'// find which output (of the three dummy variables) has the highest probability
    [~,classNet] = max(yNet',[],2);

    %// convert YTEST into a format that can be compared with classNet
    [~,classTest] = find(YTEST);


    %'// Check the success of the classifier
    cp = classperf(classTest, classNet);
    testval = cp.CorrectRate; %// replace this with your preferred metric

end

我没有神经网络工具箱所以我无法测试这个我害怕。但它应该证明这一原则。