Repository navigation
[MRG+1] intercept_scaling should be set explicitly when liblinear is used - #11763
adrinjalali wants to merge 32 commits into
Conversation
jnothman
left a comment
There was a problem hiding this comment.
Please add a test that this error is raised in the right circumstance
| if fit_intercept and intercept_scaling == 'warn': | ||
| warnings.warn("liblinear does not regularize the intercept." | ||
| " Therefore intercept_scaling should be set " | ||
| "appropriately when fit_intercept is set to True. " |
| self.warm_start = warm_start | ||
| self.n_jobs = n_jobs | ||
|
|
||
| if fit_intercept and intercept_scaling == 'warn': |
There was a problem hiding this comment.
Please move all validation to fit rather than __init__.
There was a problem hiding this comment.
I had it initially in fit, but a test failed and complained that a public attribute is being changed in fit. Apparently the policy is that only private attributes can be changed and set in fit functions. What should I do about that test?
Alternatively, I can set a local variable in fit and use that instead of changing it, which would work depending on weather or not all the relevant functions accept it as an input rather than reading it from self.
|
You'll need to fix doctests also, which expect the default value to be 1 |
|
I don't see why you need to overwrite the attribute. Just use a local
variable
|
1d5067a to
d64ac07
Compare
|
@jnothman this is of course one proposal. Alternatively, would you be opposed to moving the It's less code, so I kinda prefer it. But it has the caveat of adding the semantic of |
…rn into intercept_scaling
|
yes, do it in _fit_liblinear, but don't add default values there. default
values in private functions are mostly a source of bugs.
|
|
I guess this is now ready for review. I tried to keep the changes as minimal as I could. What do you think @jnothman ? |
| Ridge(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=None, | ||
| normalize=False, random_state=None, solver='sag', tol=0.001) | ||
|
|
||
| normalize=False, random_state=None, solver='sag', tol=0.001) |
There was a problem hiding this comment.
Why did the indentation here change?
There was a problem hiding this comment.
My bad. Probably wrong key-stroke or something. Fixed.
|
|
||
| >>> logistic = linear_model.LogisticRegression(C=1e5) | ||
| >>> logistic = linear_model.LogisticRegression(C=1e5, solver='liblinear', | ||
| ... fit_intercept=True, |
There was a problem hiding this comment.
Hmm... I think changes like this shouldn't be necessary if we merge #11476 first
There was a problem hiding this comment.
Sure, I'll rebase to master once it's merged, fix the merge conflicts and this will be gone.
| def test_intercept_scaling_warning(): | ||
| clf = svm.LinearSVC(fit_intercept=True) | ||
| assert_warns_message(UserWarning, | ||
| "liblinear does not regularize the intercept. " |
There was a problem hiding this comment.
Liblinear does regularise the intercept. That's the problem! You've inverted the statement, or I did.
There was a problem hiding this comment.
I wrote this based on your comment This is to improve awareness of liblinear's failure to regularise the intercept. And you didn't complain about this part of the text on the first review :D
Shall I change it to liblinear regularizes the intercept. ....?
There was a problem hiding this comment.
Yes, please. I assumed it was my error. Negations aren't my forté! :D
| "liblinear does not regularize the intercept. " | ||
| "Therefore intercept_scaling should be set explicitly" | ||
| " when fit_intercept is set to True. Default value of" | ||
| " 1. is used.", |
…rn into intercept_scaling
|
This looks good now @jnothman ? |
jnothman
left a comment
There was a problem hiding this comment.
I think this is good in terms of creating awareness. Others might think it is:
- too verbose: we are already introducing multiple warnings in version 0.20 when using LogisticRegression().
- unnecessary in many cases, which I think is true. It depends on the data, and the C, how sensitive the model is to intercept_scaling.
|
It'd be nice if we could get a second review on this one :) |
|
I don't want to sneak this into a release at the last minute actually. I
would rather get feedback.
|
TomDLT
left a comment
There was a problem hiding this comment.
LGTM
Indeed, it leads to a rather verbose LogisticRegression:
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
lr = LogisticRegression().fit(X, y)results in three warnings:
/cal/homes/tdupre/work/src/scikit-learn/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
FutureWarning)
/cal/homes/tdupre/work/src/scikit-learn/sklearn/linear_model/logistic.py:459: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
"this warning.", FutureWarning)
/cal/homes/tdupre/work/src/scikit-learn/sklearn/svm/base.py:870: UserWarning: liblinear regularizes the intercept. Therefore intercept_scaling should be set explicitly when fit_intercept is set to True. Default value of 1 is used.
UserWarning)
I would still be in favor of adding this warning, but the message might be a bit more specific for LogisticRegression, for instance suggesting to change the solver. In SVM, liblinear is the only solver though.
| lr.fit, iris.data, iris.target) | ||
|
|
||
| lrcv = LogisticRegressionCV(solver='liblinear', n_jobs=1) | ||
| assert_warns_message(UserWarning, |
There was a problem hiding this comment.
As this is an exact copy of above, you could factorize it with:
@pytest.mark.parametrize('klass', [LogisticRegression, LogisticRegressionCV])
def test_multinomial_validation(klass):
lr = klass(solver='liblinear')
...| " 1 is used.", | ||
| clf.fit, X, Y) | ||
|
|
||
| reg = svm.LinearSVR(fit_intercept=True) |
|
|
|
@adrinjalali let's discuss this. If done it should be done too in LogisticRegression if solver='liblinear' I am personally +0 on this |
|
@agramfort I'm happy to do the same for |
|
What's the status of this? |
|
Our concern was that this is introducing yet another warning on top of other warnings, but at the same time its a warning we need to raise. So for now we haven't done anything. |
|
I saw more recent discussion (I think on twitter) regarding this one, and the same issue on liblinear I think. Do we want to raise this warning now? |
|
Closing this one. Happy to revisit once we have a decision. |
Fixes #11715
I'll fix the other related classes once I know I'm doing it right.