You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Linear models take unreasonable longer time in certain data size. #10813
When I use Lasso to fit an artificial data set, the running time has a weird pattern: When the data size is 15900 * 500 or 161000 * 500, it takes less than 2 seconds. However, when the size is 16000 * 500, it becomes more than 20 seconds. It totally makes no sense.
The experiment is repeatable. I am using sklearn 0.19.0, I tried the same program on Windows, Mac OS and Linux, all of them has this problem.
This problem appears only when the input data is large numbers. In this example I use 1e50.
Other Linear models like Ridge also have this problem.
Steps/Code to Reproduce
fromsklearnimportlinear_modelimportnumpyasnpimporttimeestimator=linear_model.Lasso()
#dimension is fixeddimension=500#sampleNumber range from 15500 to 16500forsampleNumberinrange(15400, 16500, 100):
x=np.ones([sampleNumber, dimension]) *1e50y=np.ones([sampleNumber]) *1e50#measure running timestart=time.time()
estimator=estimator.fit(x, y)
end=time.time()
print("Sample Number:%d, Time: %f"% (sampleNumber, end-start))
Interesting, I can reproduce the behaviour and I don't know why this happens. If I were you I would try to profile to see whether you can learn something from it.
Note that if I remove the 1e50, the timings are similar for all sample numbers.
So essentially in some cases, this example does not seem to converge.
Lasso optimizes the Elastic Net objective function that is convex, and the used coordinate descent solver should theoretically always converge. Since it doesn't, the only thing I can think of is that it's due to some numerical issues. Using numbers of the order of 1e50 definitely does not help.
As suggested by @lesteve, just scaling the input data should address this issue. This is also mentionned in the user guide: agreed linear models are generally more robust to feature scaling but maybe not up to 1e50 order of magnitude...
Being robust to feature scaling is a theoretical property. All our
implementations necessarily use fixed precision numbers.
I think all we can do is try to improve the identification of bad datasets
and warn. But I'm not sure how to do that when an overflow flag is not
explicitly set.
Yes, I think adding a convergence warning would have helped; as mentioned in #10813 (comment) there is no other immediately straightforward way to detect this issue otherwise.
I'm quite new to this (I've been an sklearn end-user only so far) so I would really appreciate some guidance for my first contribution. I took a look at the code and it looks like a potential place to raise the warning could be in coordinate_descent.py, after line 481:
There the model object contains all 3 variables to check for lack of convergence (namely dual_gap_, eps_ and n_iter_).
Do you think this is a valid place or should the ConvergenceWarning be within the .pyx files directly?
we are fitting an (unregularized) intercept and y is constant. So the
optimal solution should be np.zeros(n_features), whatever the value of
alpha. This clearly makes the example a degenerate case.
If I use fit_intercept=False, I get similar timings for all n_samples.
if I use fit_intercept=True, the solution in the cases which run
fine all have 1 as first element, then nothing but zeros; estimator.dual_gap_
is equal to n_samples
For the 2 problematic solution, coef_ = 0 everywhere, and dual_gap_ is
exactly 0 which is surprising
Also as mentioned, we are fitting with alpha=1. for a problem where the
smallest alpha giving a 0 solution is 1.5e104...
Activity
lesteve commentedon Mar 15, 2018
Interesting, I can reproduce the behaviour and I don't know why this happens. If I were you I would try to profile to see whether you can learn something from it.
Note that if I remove the
1e50, the timings are similar for all sample numbers.jnothman commentedon Mar 15, 2018
lesteve commentedon Mar 15, 2018
On my machine
estimator.n_iter_is 2 in the fast cases and 1000 (i.e.n_iter_ == max_iter) in the slow cases.rth commentedon Mar 16, 2018
So essentially in some cases, this example does not seem to converge.
Lasso optimizes the Elastic Net objective function that is convex, and the used coordinate descent solver should theoretically always converge. Since it doesn't, the only thing I can think of is that it's due to some numerical issues. Using numbers of the order of 1e50 definitely does not help.
As suggested by @lesteve, just scaling the input data should address this issue. This is also mentionned in the user guide: agreed linear models are generally more robust to feature scaling but maybe not up to 1e50 order of magnitude...
jnothman commentedon Mar 17, 2018
lesteve commentedon Mar 26, 2018
Maybe a ConvergenceWarning could have helped as mentioned by @rth in #10866?
rth commentedon Apr 24, 2018
Yes, I think adding a convergence warning would have helped; as mentioned in #10813 (comment) there is no other immediately straightforward way to detect this issue otherwise.
I think
scikit-learn/sklearn/linear_model/cd_fast.pyx
Line 320 in 3e26fc6
and other
enet_coordinate_descent*solvers there should raise a convergence warning if the main loop exited without reaching the desired tolerance.kajocina commentedon May 4, 2018
Since this issue has been tagged as a good first issue, I would be happy to help on that problem!
kajocina commentedon May 7, 2018
I'm quite new to this (I've been an sklearn end-user only so far) so I would really appreciate some guidance for my first contribution. I took a look at the code and it looks like a potential place to raise the warning could be in coordinate_descent.py, after line 481:
scikit-learn/sklearn/linear_model/coordinate_descent.py
Line 481 in 622f912
There the model object contains all 3 variables to check for lack of convergence (namely dual_gap_, eps_ and n_iter_).
Do you think this is a valid place or should the ConvergenceWarning be within the .pyx files directly?
mathurinm commentedon May 7, 2018
2 additional findings:
we are fitting an (unregularized) intercept and y is constant. So the
optimal solution should be np.zeros(n_features), whatever the value of
alpha. This clearly makes the example a degenerate case.
If I use fit_intercept=False, I get similar timings for all n_samples.
if I use fit_intercept=True, the solution in the cases which run
fine all have 1 as first element, then nothing but zeros; estimator.dual_gap_
is equal to n_samples
For the 2 problematic solution, coef_ = 0 everywhere, and dual_gap_ is
exactly 0 which is surprising
Also as mentioned, we are fitting with alpha=1. for a problem where the
smallest alpha giving a 0 solution is 1.5e104...