How fitrlinear determines Lambda when 10fold cross-validation is on?

5 ビュー (過去 30 日間)
Peter He
Peter He 2022 年 3 月 3 日
コメント済み: Peter He 2022 年 4 月 1 日
Here is my code and my questions are at the end of the code.
%% Predict Values Using Ridge Regression
% Predict miles per gallon (MPG) values using ridge regression.
%
% Load the |carbig| data set.
clearvars;
load carbig
X = [Acceleration Weight Displacement Horsepower];
y = MPG;
% remove NaN entries in y
idxNaN=find(isnan(y));
X(idxNaN,:)=[];
y(idxNaN)=[];
% remove NaN entries in remaining X
[r, c] = find(isnan(X));
idxNaN=unique(c);
X(idxNaN,:)=[];
y(idxNaN)=[];
%%
% Split the data into training and test sets.
n = length(y);
rng('default') % For reproducibility
c = cvpartition(n,'HoldOut',0.3);
idxTrain = training(c,1);
idxTest = ~idxTrain;
%%
%Predict miles per gallon (MPG) values using ridge regression
mdl2=fitrlinear(X(idxTrain,:),y(idxTrain),'Learner','leastsquares','Regularization','ridge','CrossVal','on','KFold',10)
% examine one CV model
mdl2.Trained{2}
% If I change the number in {}, I get slightly different Lambda. How is it
% determined? If it's determined by the cross-validation, then test error
% can't be derived from yhat.
% I assume that I cannot use predict function on the test data based on my
% understanding.
% yhat2=predict(mdl2,X(idxTest,:));
% what is the best way to train a model using the entire training data
% (i.e., idxTrain) based on the cross-validation models?
  2 件のコメント
Ive J
Ive J 2022 年 3 月 3 日
編集済み: Ive J 2022 年 3 月 3 日
"If I change the number in {}, I get slightly different Lambda. How is it determined?"
I suggest reading the doc, especially the "Find Good Lasso Penalty Using Cross-Validation" example. Your mdl2 contains 10 models from 10-fold CV, each was run on different chunk (non-overlapping) of data, so of course, they're different.
Peter He
Peter He 2022 年 4 月 1 日
Now I got it. Thanks!

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeSupport Vector Machine Regression についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by