I am using LassoLarsCV from sklearn on a dataset with around 100 variables. After fitting the model around 80 of them have a coefficient of 0. I want to remove those variables from my dataset because it requires unnecessary load to the DB and network to request them every time during prediction.

After refitting LassoLarsCV with the same settings on the reduced dataset I get different coefficients and R2 score on my test dataset.

Is this behavior expected? What would be a better approach to not change the actual model, can i remove manually the 0-coefficients from the model object or would it have side effects on the internal model structure?


1 Answer 1


Is this behavior expected?

Yes. By removing the variables, you are changing the loss function, and so the optimization will be different.

What would be a better approach

You could pass the full data to the model. This would be the easiest but it seems you aren't interested in doing that. Because the Lasso is a linear model, it would not be so hard to write your own function to compute the predictions. That way, you only need to pull the data you need and the computation is just a matrix vector product.

  • $\begingroup$ If there is no other solution I will try to change and overwrite the coef_ array manually, knowing that it's not a very clean solution. But mathematically it should be identical to the fitted model then. $\endgroup$
    – HansHupe
    Commented Sep 23, 2020 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.