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.

댓글을 달려면 로그인하십시오.

답변 (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