How fitrlinear determines Lambda when 10fold cross-validation is on?
Show older comments
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 Comments
"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
on 1 Apr 2022
Answers (0)
Categories
Find more on Support Vector Machine Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!