Matlab虹膜分类输入大小不匹配

时间:2014-05-02 12:13:39

标签: matlab input classification

我是Matlab的新手。我想要做的是使用交叉验证对虹膜数据集进行分类(这意味着我必须将数据集拆分为3:trainingSet,validationSet和测试集)。在我看来,我在这里写的一切都很好(有时候初学者很难)。所以我可以用一些帮助...

这是分割数据的功能(前35个(70%的数据)是训练集,其余是验证集(15%)和15%我稍后将用于测试集)

close all; clear ;
load fisheriris;

for i = 1:35
    for j = 1:4
        trainSeto(i,j) = meas(i,j);
    end
end
for i = 51:85
    for j = 1:4
        trainVers(i-50,j) = meas(i,j);
    end
end
for i = 101:135
    for j = 1:4
        trainVirg(i-100,j) = meas(i,j);
    end
end
for i = 36:43
    for j = 1:4
        valSeto(i-35,j) = meas(i,j);
    end
end
for i = 86:93
    for j = 1:4
        valVers(i-85,j) = meas(i,j);
    end
end
for i = 136:143
    for j = 1:4
        valVirg(i-135,j) = meas(i,j);
    end
end
for i = 44:50
    for j = 1:4
        testSeto(i-43,j) = meas(i,j);
    end
end
for i = 94:100
    for j = 1:4
        testVers(i-93,j) = meas(i,j);
    end
end
for i = 144:150
    for j = 1:4
        testVirg(i-143,j) = meas(i,j);
    end
end

这是主要的脚本:

close all; clear;
%%the 3 tipes of iris
run divinp

% the representation of the 3 classes(their coding)
a = [-1 -1 +1]';
b = [-1 +1 -1]';
c = [+1 -1 -1]';

%training set
trainInp = [trainSeto trainVers trainVirg];

%the targets
T = [repmat(a,1,length(trainSeto)) repmat(b,1,length(trainVers)) repmat(c,1,length(trainVirg))];

%%the training

trainCor = zeros(10,10);
valCor = zeros(10,10);
Xn = zeros(1,10);
Yn = zeros(1,10);

for k = 1:10,
    Yn(1,k) = k; 
    for n = 1:10,
        Xn(1,n) = n;
        net = newff(trainInp,T,[k n],{},'trainbfg');
        net = init(net);
        net.divideParam.trainRatio = 1;
        net.divideParam.valRatio = 0;
        net.divideParam.testRatio = 0;
        net.trainParam.max_fail = 2;

        valInp = [valSeto valVers valVirg];
        valT = [repmat(a,1,length(valSeto)) repmat(b,1,length(valVers)) repmat(c,1,length(valVirg))];

        [net,tr] = train(net,trainInp,T);

        Y = sim(net,trainInp);

        [Yval,Pfval,Afval,Eval,perfval] = sim(net,valInp,[],[],valT);

        % calculate [%] of correct classifications
        trainCor(k,n) = 100 * length(find(T.*Y > 0)) / length(T);
        valCor(k,n) = 100 * length(find(valT.*Yval > 0)) / length(valT);
        end
        end
        figure
        surf(Xn,Yn,trainCor/3);
        view(2)
        figure
        surf(Xn,Yn,valCor/3);
        view(2)

我收到此错误

  

使用trainbfg时出错(第120行)输入和目标有所不同   样本数量。

     

网络/火车出错(第106行)[net,tr] =   feval(net.trainFcn,网,X,T,僖,艾,EW,net.trainParam);

     

ClassIris中的错误(第38行)           [net,tr] = train(net,trainInp,T);

close all; clear ;
load fisheriris;

trainSetoIndx = 1:35;
trainVersIndx = 51:85;   % or: trainVersIndx = trainSetoIndx + 50;
trainVirgIndx = 101:135;

colIndx = 1:4;

trainSeto = meas(trainSetoIndx, colIndx);
trainVers = meas(trainVersIndx, colIndx);
trainVirg = meas(trainVirgIndx, colIndx);

valSetoIndx = 36:43;
valVersIndx = 86:93;
valVirgIndx = 136:143

valSeto = meas(valSetoIndx, colIndx);
valVers = meas(valVersIndx, colIndx);
valVirg = meas(valVirgIndx, colIndx);

testSetoIndx = 44:50;
testVersIndx = 94:100;
testVirgIndx = 144:150

testSeto = meas(testSetoIndx, colIndx);
testVers = meas(testVersIndx, colIndx);
testVirg = meas(testVirgIndx, colIndx);

我用“:”写了它仍然是同样的问题它是与repmat的东西..我不知道如何正确使用它或者newff:D

1 个答案:

答案 0 :(得分:0)

为了让您入门,您可以按如下方式重写代码循环:

trainSetoIndx = 1:35;
trainVersIndx = 51:85;   % or: trainVersIndx = trainSetoIndx + 50;
trainVirgIndx = 101:135; % or: trainVirgIndx = trainSetoIndx + 100;

colIndx = 1:4; % can't tell if this is all the columns in meas

trainSeto = meas(trainIndx, colIndx);
trainVers = meas(trainVersIndx, colIndx);
trainVirg = meas(trainVirgIndx, colIndx);

对所有其他人做同样的事情:

valSetoIndx = 36:43;

接下来,只需在命令提示符下键入whos,您将看到已创建的所有阵列的大小。看看那些需要相同尺寸的尺寸实际上是否具有相同的尺寸。