首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MATLAB中神经网络分类十次交叉验证实例

MATLAB中神经网络分类十次交叉验证实例
EN

Stack Overflow用户
提问于 2016-01-11 14:55:43
回答 1查看 6.4K关注 0票数 2

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

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

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

代码语言:javascript
复制
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给出的错误:

代码语言:javascript
复制
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)');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-14 07:56:30

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

代码语言:javascript
复制
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函数提供的,所以您不需要担心自己的数据分割),在训练集上训练一个神经网络,在测试集上测试它,然后使用您喜欢的度量输出一个分数。所以就像这样:

代码语言:javascript
复制
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

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34724463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档