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

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Support 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