Index in position 2 exceeds array bounds using crossval

1 回表示 (過去 30 日間)
Tessa Kol
Tessa Kol 2020 年 10 月 20 日
コメント済み: Tessa Kol 2020 年 10 月 23 日
Dear all,
I got the following error when using crossval.
Error using crossval>evalFun (line 488)
The function '@(Xtr,Ytr,Xte)predict(fitlm(Xtr,Ytr(:,2)),Xte)' generated the following error:
Index in position 2 exceeds array bounds (must not exceed 1).
Error in crossval>getLossVal (line 525)
funResult = evalFun(funorStr,arg(1:end-1));
Error in crossval (line 424)
[funResult,outarg] = getLossVal(i, nData, cvp, data, predfun);
This is my code. The X and Y are stored in the attached .mat file.
%% Cross validation
hpartition = cvpartition(81,'Holdout',0.2);
idxTrain = training(hpartition);
Xtr = X(idxTrain,:);
Ytr = Y(idxTrain,:);
idxNew = test(hpartition);
Xte = X(idxNew,:);
for i = 1:2
for j = 1:3
fcn{i,1} = @(Xtr, Ytr, Xte) predict(fitlm(Xtr,Ytr(:,i)), Xte);
fcn{i,2} = @(Xtr, Ytr, Xte) predict(fitrsvm(Xtr,Ytr(:,i)), Xte);
fcn{i,3} = @(Xtr, Ytr, Xte) predict(fitrgp(Xtr,Ytr(:,i)), Xte);
mse(i,j) = crossval('mse', X, Y(:,i),'Predfun',fcn{i,j}, 'kfold',10);
end
end

採用された回答

Nagasai Bharat
Nagasai Bharat 2020 年 10 月 23 日
Hi,
The problem in above code was that crossval does the train-test spilt and it takes in the Ytr as column vector but you did provide Ytr are a 2-D Matrix due to which we were getting that array bound error.The below code snippet should resolve and simplify your issue.
load data
% hpartition = cvpartition(81,'Holdout',0.2);
% idxTrain = training(hpartition);
% Xtr = X(idxTrain,:);
% Ytr = Y(idxTrain,:);
% % Ytr_1 = Y(idxTrain,1);
% % Ytr_2 = Y(idxTrain,2);
% idxNew = test(hpartition);
% Xte = X(idxNew,:);
for i=1:2
for j = 1:3
fcn{i,1} = @(Xtr, Ytr, Xte) predict(fitlm(Xtr,Ytr), Xte);
fcn{i,2} = @(Xtr, Ytr, Xte) predict(fitrsvm(Xtr,Ytr), Xte);
fcn{i,3} = @(Xtr, Ytr, Xte) predict(fitrgp(Xtr,Ytr), Xte);
mse(i,j) = crossval('mse', X, Y(:,i),'Predfun',fcn{i,j}, 'kfold',10);
end
For more information you could follow crossval documentation.
  1 件のコメント
Tessa Kol
Tessa Kol 2020 年 10 月 23 日
Thank for your reply. I discovered indeed that issue was the 2D-matrix. I forgot to close this post after I resolved the issue myself. But thank you so much for providing the answer!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGaussian Process Regression についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by