Skip to content

[MRG+1] intercept_scaling should be set explicitly when liblinear is used - #11763

Closed
adrinjalali wants to merge 32 commits into
scikit-learn:masterfrom
adrinjalali:intercept_scaling
Closed

adrinjalali wants to merge 32 commits into
scikit-learn:masterfrom
adrinjalali:intercept_scaling

Conversation

@adrinjalali

Copy link
Copy Markdown
Member

Fixes #11715

I'll fix the other related classes once I know I'm doing it right.

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that this error is raised in the right circumstance

Comment thread sklearn/linear_model/logistic.py Outdated
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. "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appropriately -> explicitly

Comment thread sklearn/linear_model/logistic.py Outdated
self.warm_start = warm_start
self.n_jobs = n_jobs

if fit_intercept and intercept_scaling == 'warn':

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move all validation to fit rather than __init__.

@adrinjalali adrinjalali Aug 7, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jnothman

jnothman commented Aug 7, 2018

Copy link
Copy Markdown
Member

You'll need to fix doctests also, which expect the default value to be 1

@jnothman

jnothman commented Aug 7, 2018 via email

Copy link
Copy Markdown
Member

@adrinjalali

Copy link
Copy Markdown
Member Author

@jnothman this is of course one proposal. Alternatively, would you be opposed to moving the intercept_scaling check to _fit_liblinear in svm/base.py, so that everybody calling it with warn would get the warning? Then I'll just need to add the tests for all the classes calling it, and set their defaults to warn.

It's less code, so I kinda prefer it. But it has the caveat of adding the semantic of if intercept_scaling == 'warn' then intercept_scaling = 1 to _fit_linear, and adding some default values to the parameters after it which don't have any default value yet, i.e. class_weight, penalty, dual, verbose, max_iter, tol.

@jnothman

jnothman commented Aug 7, 2018 via email

Copy link
Copy Markdown
Member

@adrinjalali

Copy link
Copy Markdown
Member Author

I guess this is now ready for review. I tried to keep the changes as minimal as I could. What do you think @jnothman ?

@adrinjalali adrinjalali changed the title [WIP] intercept_scaling should be set explicitly when liblinear is used [MRG] intercept_scaling should be set explicitly when liblinear is used Aug 8, 2018
Comment thread sklearn/linear_model/sag.py Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did the indentation here change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I think changes like this shouldn't be necessary if we merge #11476 first

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll rebase to master once it's merged, fix the merge conflicts and this will be gone.

Comment thread sklearn/svm/tests/test_svm.py Outdated
def test_intercept_scaling_warning():
clf = svm.LinearSVC(fit_intercept=True)
assert_warns_message(UserWarning,
"liblinear does not regularize the intercept. "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Liblinear does regularise the intercept. That's the problem! You've inverted the statement, or I did.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. ....?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please. I assumed it was my error. Negations aren't my forté! :D

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread sklearn/svm/tests/test_svm.py Outdated
"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.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The . here is unhelpful

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@adrinjalali

Copy link
Copy Markdown
Member Author

This looks good now @jnothman ?

@jnothman jnothman left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[]

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good in terms of creating awareness. Others might think it is:

  1. too verbose: we are already introducing multiple warnings in version 0.20 when using LogisticRegression().
  2. 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.

@adrinjalali adrinjalali changed the title [MRG] intercept_scaling should be set explicitly when liblinear is used [MRG+1] intercept_scaling should be set explicitly when liblinear is used Aug 30, 2018
@adrinjalali

Copy link
Copy Markdown
Member Author

It'd be nice if we could get a second review on this one :)
Also, should the warning be mentioned in the docs and/or whats_new somewhere? (I'm not aware of scikit-learn's convention on this matter).

@jnothman

jnothman commented Sep 8, 2018 via email

Copy link
Copy Markdown
Member

@TomDLT TomDLT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')
    ...

Comment thread sklearn/svm/tests/test_svm.py Outdated
" 1 is used.",
clf.fit, X, Y)

reg = svm.LinearSVR(fit_intercept=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@jnothman

Copy link
Copy Markdown
Member

intercept_scaling may only have real affect when there is substantial regularisation. This might deserve some benchmarking to work out when intercept regularisation is likely to be a big problem, and whether there are ways to not warn when it is unlikely a problem...

@agramfort

Copy link
Copy Markdown
Member

@adrinjalali let's discuss this. If done it should be done too in LogisticRegression if solver='liblinear'

I am personally +0 on this

@adrinjalali

Copy link
Copy Markdown
Member Author

@agramfort I'm happy to do the same for liblinear (unfortunately I saw this a few days too late :/). We kinda need more votes on this one to find a consensus I guess. I'd be +1 if we hadn't introduced tons of new warnings. but now I'm not sure.

@amueller amueller added the Needs Decision Requires decision label Aug 6, 2019
@amueller

amueller commented Aug 6, 2019

Copy link
Copy Markdown
Member

What's the status of this?

@adrinjalali

Copy link
Copy Markdown
Member Author

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.

@adrinjalali

Copy link
Copy Markdown
Member Author

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?

@adrinjalali

Copy link
Copy Markdown
Member Author

Closing this one. Happy to revisit once we have a decision.

@adrinjalali
adrinjalali deleted the intercept_scaling branch May 19, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Decision Requires decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

intercept_scaling should be set explicitly

5 participants