Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Peter He
Peter He am 3 Mär. 2022
Kommentiert: Peter He am 1 Apr. 2022
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 Kommentare
Ive J
Ive J am 3 Mär. 2022
Bearbeitet: Ive J am 3 Mär. 2022
"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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by