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
Sparse Ridge regression with intercept is incorrect #4710
Ridge regression with fit_intercept=True does not give the same result if X is dense or sparse.
The call to _center_data in _BaseRidge.fit should probably be a call to sparse_center_data
test example :
importnumpyasnpimportscipy.sparseasspfromsklearn.linear_modelimportRidgefromsklearn.utilsimportsafe_sqrdefget_pobj(w, intercept, myX, myy, alpha):
w=w.ravel()
p=np.sum(safe_sqr(myX.dot(w) +intercept-myy)) /2.p+=alpha*w.dot(w) /2.returnpdeftest_ridge(X, y, alpha):
forsolverin ["cholesky", "lsqr", "sparse_cg"]:
clf=Ridge(alpha=alpha, tol=1.0e-15, solver=solver,
fit_intercept=True)
clf.fit(X, y)
printget_pobj(clf.coef_, clf.intercept_, X, y, alpha)
alpha=1.0r=np.random.RandomState(42)
X=r.randn(100000, 2)
w=r.randn(2)
i=10y=np.dot(X, w) +iprintget_pobj(w, i, X, y, alpha)
print"----Dense----"test_ridge(X, y, alpha)
print"----Sparse---"X=sp.csr_matrix(X)
test_ridge(X, y, alpha)
The way the intercept fitting is currently implemented cannot work for sparse data because the data sparsity would be lost when applying the centering.
The solution would be IMO to fit the intercept term directly in the solvers without centering the data before hand.
Since the problem is jointly convex in coef and intercept, the procedure converges to the global minimum. An advantage is that this works with all solvers unmodified. There is just a bit of work to make sure that sample_weight and multi-output support work correctly.
I'm not sure the issue is fixed when X does not have zero mean? note that make_regression, with bias=10, as used in the test, produces a Y with non-zero mean, but an X with zero mean.
Yes, the behaviour is fixed. Here are results of the test from the top of the issue (by @TomDLT) when run locally against PR #13363
1.224112693590405
----Dense----
1.2241004921533805
1.2241004921533807
1.2241004921533807
----Sparse---
/home/bartosz/repos/libraries/scikit-learn/sklearn/linear_model/ridge.py:387: UserWarning: In Ridge, only 'sag' solver can directly fit the intercept. Solver has been automatically changed into 'sag'.
warnings.warn("In Ridge, only 'sag' solver can directly fit the "
1.2241004921533807
/home/bartosz/repos/libraries/scikit-learn/sklearn/linear_model/ridge.py:387: UserWarning: In Ridge, only 'sag' solver can directly fit the intercept. Solver has been automatically changed into 'sag'.
warnings.warn("In Ridge, only 'sag' solver can directly fit the "
1.2241004921533807
1.2241004921533807
with alpha=0
0.0
----Dense----
1.539028183040933e-26
9.320391595186254e-28
4.7141538835102444e-26
----Sparse---
/home/bartosz/repos/libraries/scikit-learn/sklearn/linear_model/ridge.py:387: UserWarning: In Ridge, only 'sag' solver can directly fit the intercept. Solver has been automatically changed into 'sag'.
warnings.warn("In Ridge, only 'sag' solver can directly fit the "
1.4816503850996827e-25
/home/bartosz/repos/libraries/scikit-learn/sklearn/linear_model/ridge.py:387: UserWarning: In Ridge, only 'sag' solver can directly fit the intercept. Solver has been automatically changed into 'sag'.
warnings.warn("In Ridge, only 'sag' solver can directly fit the "
7.156948451226682e-25
6.591011749212076e-26
Note that I ran the test against PR #13363 (which is not yet merged).
More precisely, it is fixed for "sparse_cg" (#13336) and "sag" solvers. It is not fixed for "cholesky" and "lsqr" solvers, and calling these solvers on sparse data with intercept=True results in using "sag" solver instead (with a warning, see #5360).
Activity
agramfort commentedon May 12, 2015
mblondel commentedon May 12, 2015
The way the intercept fitting is currently implemented cannot work for sparse data because the data sparsity would be lost when applying the centering.
The solution would be IMO to fit the intercept term directly in the solvers without centering the data before hand.
For the sparse_cg solver, it might be possible to center the data just in time when computing the matrix vector product here:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/linear_model/ridge.py#L39
and here:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/linear_model/ridge.py#L44
mblondel commentedon May 14, 2015
Updating all solvers is a lot of work. As a quick temporary fix, we could use an alternating minimization like this:
Since the problem is jointly convex in coef and intercept, the procedure converges to the global minimum. An advantage is that this works with all solvers unmodified. There is just a bit of work to make sure that sample_weight and multi-output support work correctly.
agramfort commentedon May 14, 2015
mblondel commentedon May 15, 2015
If we also add warm_start support to
ridge_regression, I would expect the alternating minimization procedure to be pretty fast.FIX temporary fix for scikit-learn#4710
amueller commentedon Oct 14, 2015
I guess we keep this open for the better fix? But I'll untag 0.17 as #5360 is merged.
btel commentedon Feb 27, 2019
as discussed with @GaelVaroquaux I will implement intercept fitting with
sparse_cgjeromedockes commentedon Feb 28, 2019
I'm not sure the issue is fixed when X does not have zero mean? note that
make_regression, with bias=10, as used in the test, produces a Y with non-zero mean, but an X with zero mean.see for example
#13325 (comment)
jnothman commentedon Apr 15, 2019
@btel, what's the status of this?
btel commentedon Apr 16, 2019
@jnothman intercept with sparse_cg was implemented in PR #13336 (merged).
jnothman commentedon Apr 16, 2019
So this should be closed? This issue implies that there was incorrect behaviour not just that the use case wasn't supported
btel commentedon Apr 16, 2019
Yes, the behaviour is fixed. Here are results of the test from the top of the issue (by @TomDLT) when run locally against PR #13363
with alpha=0
Note that I ran the test against PR #13363 (which is not yet merged).
TomDLT commentedon Apr 17, 2019
More precisely, it is fixed for "sparse_cg" (#13336) and "sag" solvers. It is not fixed for "cholesky" and "lsqr" solvers, and calling these solvers on sparse data with
intercept=Trueresults in using "sag" solver instead (with a warning, see #5360).jnothman commentedon Apr 17, 2019
Right. I'd forgotten those details. Thanks for clarifying. Let's close!