how to find minimum value of output corresponds to which input values in a loop/nested loop ? Error i am getting is Index exceeds the number of array elements.

조회 수: 1 (최근 30일)
i want to find for what value of c and what value of hh , the genError is minimum. The error i am getting is Index exceeds the number of array elements. Index must not exceed 1.
load fisheriris
X = meas;
Y = species;
TTT=0;
for c=[0.05 0.1]
for hh=[0.1 1]
TTT=TTT+1;
t = templateSVM('Standardize',true,'KernelFunction','gaussian','kernelscale',c,'boxconstraint',hh);
Mdl = fitcecoc(X,Y,'Learners',t,'FitPosterior',true,...
'ClassNames',{'setosa','versicolor','virginica'},...
'Verbose',2);
CVMdl = crossval(Mdl);
genError(TTT) = kfoldLoss(CVMdl);
end
end
[mingenaratederror min_index] = min(genError);
c_kscale = c(min_index); % get the corresponding value from the c array
hh_boxcon = hh(min_index); % get the corresponding value from the hh array

채택된 답변

Voss
Voss 2022년 3월 18일
Note that after a for loop completes, the value of the loop iterator is the value it had during the last iteration of the loop:
TTT=0;
for c=[0.05 0.1]
for hh=[0.1 1]
TTT=TTT+1;
end
end
c % last value of c is 0.1
c = 0.1000
hh % last value of hh is 1
hh = 1
(An exception to this is when the loop iterates over an empty array, in which case the iterator is empty after the loop, but that's not the case here.)
The point is that c and hh are scalars, so indexing them with min_index can generate the error you saw. If you want to find the values of c and hh that correspond to the minimum value of genError, it may be convenient to create c and hh as vectors with the appropriate sequences of values:
[hh,c] = ndgrid([0.1 1],[0.05 0.1]);
c = c(:)
c = 4×1
0.0500 0.0500 0.1000 0.1000
hh = hh(:)
hh = 4×1
0.1000 1.0000 0.1000 1.0000
Now c and hh have all the values they need, in the same order as they have in your nested loops, so you can replace your nested loops with a single loop, and the single loop can use TTT to index into c and hh:
load fisheriris
X = meas;
Y = species;
for TTT = 1:numel(c)
t = templateSVM( ...
'Standardize',true, ...
'KernelFunction','gaussian', ...
'kernelscale',c(TTT), ... % c is a vector now: need to index with TTT
'boxconstraint',hh(TTT)); % same for hh
Mdl = fitcecoc(X,Y, ...
'Learners',t, ...
'FitPosterior',true,...
'ClassNames',{'setosa','versicolor','virginica'},...
'Verbose',2);
CVMdl = crossval(Mdl);
genError(TTT) = kfoldLoss(CVMdl);
end
Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM). Training binary learner 1 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 2 Positive class indices: 1 Fitting posterior probabilities for learner 1 (SVM). Training binary learner 2 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 1 Fitting posterior probabilities for learner 2 (SVM). Training binary learner 3 (SVM) out of 3 with 50 negative and 50 positive observations. Negative class indices: 3 Positive class indices: 2 Fitting posterior probabilities for learner 3 (SVM).
Now finding the values of c and hh corresponding to the minimum value of genError will work correctly:
[mingenaratederror min_index] = min(genError);
c_kscale = c(min_index); % get the corresponding value from the c array
hh_boxcon = hh(min_index); % get the corresponding value from the hh array
disp(min_index);
4
disp(c_kscale);
0.1000
disp(hh_boxcon);
1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by