LASSO function not behaving as expected

7 vues (au cours des 30 derniers jours)
N B
N B le 19 Jan 2021
Réponse apportée : N B le 20 Jan 2021
I've been performing LASSO regulerisation but it has been behaving oddly. Code that once works no longer is, and now I can't even get the example from Matlab help to work. For instance here's some data:
rng default % For reproducibility
X = randn(100,5);
weights = [0;2;0;-3;0]; % Only two nonzero coefficients
y = X*weights + randn(100,1)*0.1; % Small added noise
Following he documentation verbatim,
B = lasso(X,y);
B(:,25)
returns the error:
Index in position 2 exceeds array bounds (must not exceed 6).
because when I run it the results are:
B =
0 0 0 0 -0.0067 -0.0067
0 0 1.9802 1.9838 1.9919 1.9920
0 0 0 -0.0039 -0.0109 -0.0109
0 -0.6818 -2.9817 -2.9865 -2.9958 -2.9959
0 0 0 0 0 0.0001
So is the documentation incorrect or is something else going on here?
Additionally, the function doesn't seem to accept values for lambda, for example:
B = lasso(X,y)
C = lasso(X,y,'Lambda', 0.18)
D = lasso(X,y,'Lambda',linspace(0,1))
gives:
B =
0 0 0 0 -0.0067 -0.0067
0 0 1.9802 1.9838 1.9919 1.9920
0 0 0 -0.0039 -0.0109 -0.0109
0 -0.6818 -2.9817 -2.9865 -2.9958 -2.9959
0 0 0 0 0 0.0001
C =
0 0 0 0 -0.0067 -0.0067
0 0 1.9802 1.9838 1.9919 1.9920
0 0 0 -0.0039 -0.0109 -0.0109
0 -0.6818 -2.9817 -2.9865 -2.9958 -2.9959
0 0 0 0 0 0.0001
Operands to the || and && operators must be convertible to logical scalar values.
Error in larsen (line 197)
if storepath && size(b,2) > step
Error in lasso (line 119)
[b steps] = larsen(X, y, 0, stop, Gram, storepath, verbose);
C is returning an identical result despite a single value for lambda being given. And why is D producing an error - especially when this is the syntax given in the LASSO help page.
Any pointers would be appreciated.
  2 commentaires
Image Analyst
Image Analyst le 19 Jan 2021
Give us the code, in a single snippet, that runs and gives the error, because this:
rng default % For reproducibility
X = randn(100,5);
weights = [0;2;0;-3;0]; % Only two nonzero coefficients
y = X*weights + randn(100,1)*0.1; % Small added noise
B = lasso(X,y);
B(:,25)
runs without any error at all in R2020b.
N B
N B le 20 Jan 2021
Thanks. That's the exact snippet for which I get an error, on R2019a.
I tried the same snippet on the same version but different computer and it worked. What else could be influencing this function? Maybe I've somehow got two functions called lasso (I certainly didn't write one)?
Edit: yes, that's it. I had two functions called lasso. Not sure I want to delete the extra one that I picked up from somewhere. Now the question is what's the best way of specifying which function I want to use

Connectez-vous pour commenter.

Réponse acceptée

N B
N B le 20 Jan 2021
I had two functions called lasso, the inbuilt one and an additional one I had picked up from somewhere, and was calling the wrong one. Let this be a lesson in managing, and naming, functions and paths.

Plus de réponses (0)

Catégories

En savoir plus sur Help and Support dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by