matlab 中的套索回归
Lasso regression in matlab
我在 matlab 2013a 中使用 lasso
函数。它的工作原理如下:
X = randn(100,5);
r = [0;2;0;-3;0];
Y = X*r + randn(100,1)*.1;
%Construct the lasso fit using ten-fold cross validation. Include the FitInfo
%output so you can plot the result.
[B FitInfo] = lasso(X,Y,'CV',10); %B is a p-by-L matrix, where p is the %number of predictors (columns) in X, and L is the number of Lambda values
%Plot the cross-validated fits.
lassoPlot(B,FitInfo,'PlotType','CV');
绿色圆圈和虚线定位了具有最小交叉验证错误的 Lambda。蓝色圆圈和虚线定位了交叉验证误差最小加上一个标准差的点。
所以我的理解是绿色圆圈对应于最小化错误的 lambda 的最佳值。 但是我如何找到"automatically"(不需要画图)图中绿色圆圈的lambda值对应的向量B?
非常感谢任何帮助!
根据 documentation 它应该在 FitInfo.Lambda
中,这是一个包含 lambda 的 1xL
向量。您可能可以使用 min(FitInfo.Lambda)
找到它。
如果将 CV
名称-值对设置为 cross validate
,FitInfo
结构包含其他字段:FitInfo.LambdaMinMSE
,这正是您要查找的值。
感谢@Christina,这是一种稍微紧凑的写法:
bestValue = find(FitInfo.Lambda == FitInfo.LambdaMinMSE)
这将为您提供数组中最小 lambda 所在的索引 L
我在 matlab 2013a 中使用 lasso
函数。它的工作原理如下:
X = randn(100,5);
r = [0;2;0;-3;0];
Y = X*r + randn(100,1)*.1;
%Construct the lasso fit using ten-fold cross validation. Include the FitInfo
%output so you can plot the result.
[B FitInfo] = lasso(X,Y,'CV',10); %B is a p-by-L matrix, where p is the %number of predictors (columns) in X, and L is the number of Lambda values
%Plot the cross-validated fits.
lassoPlot(B,FitInfo,'PlotType','CV');
绿色圆圈和虚线定位了具有最小交叉验证错误的 Lambda。蓝色圆圈和虚线定位了交叉验证误差最小加上一个标准差的点。
所以我的理解是绿色圆圈对应于最小化错误的 lambda 的最佳值。 但是我如何找到"automatically"(不需要画图)图中绿色圆圈的lambda值对应的向量B?
非常感谢任何帮助!
根据 documentation 它应该在 FitInfo.Lambda
中,这是一个包含 lambda 的 1xL
向量。您可能可以使用 min(FitInfo.Lambda)
找到它。
如果将 CV
名称-值对设置为 cross validate
,FitInfo
结构包含其他字段:FitInfo.LambdaMinMSE
,这正是您要查找的值。
感谢@Christina,这是一种稍微紧凑的写法:
bestValue = find(FitInfo.Lambda == FitInfo.LambdaMinMSE)
这将为您提供数组中最小 lambda 所在的索引 L