在 MATLAB 中,我们可以使用以下命令执行交叉验证的 LASSO
[w, FitInfo] = lasso(X, y, 'CV', 3);
并获得最佳权重
w(:,FitInfo.IndexMinMSE)
我们如何找到决定系数?
最佳答案
如果我理解得很好的话MATLAB documentation和 coefficient of determination definition ,我认为下面的代码应该可以完成这项工作:
optimal_weights = w(:,FitInfo.IndexMinMSE);
SStot = var(y)*length(y);
predicted_values = X*optimal_weights;
SSres = sum( (y(:)-predicted_values(:)).^2 );
R2 = 1 - SSres/SStot;
注意SStot
可以使用sum()
计算,但我认为使用var()
会更快一些。
关于matlab - 如何从 MATLAB 中的 LASSO 拟合获得决定系数 (R^2)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28096155/