matlab中的套索回归

时间:2015-09-01 14:53:46

标签: matlab matlab-figure linear-regression

我在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');

enter image description here

绿色圆圈和虚线以最小的交叉验证错误定位Lambda。蓝色圆圈和虚线定位点,交叉验证误差最小,加上一个标准偏差。

所以我理解的是,绿色圆圈对应lambda的最佳值,使误差最小化。 但我如何能够自动找到#34; (不需要绘制图形)矢量B对应于图中绿色圆圈的λ值。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

根据documentation,它应该在FitInfo.Lambda中,这是一个包含lambdas的1xL向量。您可以使用min(FitInfo.Lambda)找到它。 如果您将CV名称 - 值对设置为cross validate,则FitInfo结构会包含其他字段:FitInfo.LambdaMinMSE,这是您正在寻找的确切值。< / p>

感谢@Christina,这是一种稍微复杂的写作方式:

bestValue = find(FitInfo.Lambda == FitInfo.LambdaMinMSE)

这将为您提供最小lambda位于数组L

中的索引