Skip to content

Use scipy.special.expit for the inverse of the logit function #12914

Closed

Description

@rth

In a few places in the code the expit function is manually implemented. For instance,

  • sklearn/linear_model/base.py:_predict_proba_lr
    prob *= -1
    np.exp(prob, prob)
    prob += 1
    np.reciprocal(prob, prob)
  • `sklearn/discriminant_analysis.py:predict_proba:L521
  • sklearn/utils/tests/test_extmath.py
    447:        return np.log(1 / (1 + np.exp(-x)))
    
  • (maybe other places I missed)

It might be better to use scipy.special.expit instead which is simpler and also appears to be faster,

In [3]: from scipy.special import expit

In [4]: import numpy as np

In [14]: y0 = np.random.rand((1000))                                                                                                                         

In [15]: %%timeit 
    ...:  
    ...: y = y0.copy() 
    ...: y *= -1 
    ...: np.exp(y, y) 
    ...: y += 1 
    ...: np.reciprocal(y, y) 
    ...:  
    ...:                                                                                                                                                     
24.8 µs ± 123 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [16]: %timeit y0.copy()                                                                                                                                   
544 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [17]: %timeit expit(y)                                                                                                                                    
18 µs ± 16.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

@ZaydH would you be interesting in taking this, following your PR #12909?

Activity

  1. added
    Easy
    good first issue
    on Jan 3, 2019
  2. qdeffense commented on Jan 3, 2019

    @qdeffense
    Contributor

    I can take this if nobody is on it

  3. rth commented on Jan 3, 2019

    @rth
    MemberAuthor

    Sure, thanks @qdeffense.

  4. ZaydH commented on Jan 3, 2019

    @ZaydH
    Contributor

    I would be happy to take it too depending of @qdeffense 's preference. This was my first time working on a project of this scale so it was a good experience for me. I am happy to defer either way.

  5. AbhishekBabuji commented on Jan 5, 2019

    @AbhishekBabuji

    I just want to go over how you would fix this. I've never submitted to Open Source before. So I would like to convey my thought process on how I would solve this issue. I'm not going to start working on contributing here until I feel confident enough (even for first time help wanted tags)

    I would go to as many .py files in the folder as I can to find the code that represents the inverse of the logit function and replace it with expit(y). I think if you're using an editor like Atom, and you have a text, it can search over an entire project folder for a specific sequence of text?

    Would this one of the ways you would go about resolving the issue?

  6. ZaydH commented on Jan 5, 2019

    @ZaydH
    Contributor

    @AbhishekBabuji , your idea looks very reasonable to me although I have never used Atom myself so I cannot speak to it specifically. If I were to go about checking, I would use something like grep to find all usages of np.exp (or some similar regex).

    Also if you have something in the code like 1. / (1. + np.exp(f(x))) you would need to replace it with expit(-f(x)) (observe the negation).

  7. ZaydH commented on Jan 5, 2019

    @ZaydH
    Contributor

    @qdeffense - Can you let me know if you already started on this? If you haven't, I will begin working on it straightaway. If I do not hear by Monday, I will jump on it then. Please let me know.

  8. qdeffense commented on Jan 6, 2019

    @qdeffense
    Contributor

    @ZaydH I just found 2 more files where the function is implemented :
    benchmarks/bench_rcv1_logreg_convergence.py : L28
    and
    sklearn/linear_model/tests/test_sag.py : L45

    I'll keep looking with a wider search

    Can you do the changes ? I have troubles building the project so I won't be able to run tests until I fix my problem

  9. RonLek commented on Jan 6, 2019

    @RonLek
    Contributor

    @qdeffense @ZaydH I found some more files like sklearn/linear_model/plot_logistic.py , benchmarks/bench_isotonic.py, sklearn/ensemble/tests/test_gradient_boosting.py, sklearn/tests/test_isotonic.py, examples/ensemble/plot_gradient_boosting_oob.py where the function can be implemented and have made a PR.
    @qdeffense benchmarks/bench_rcv1_logreg_convergence.py : L28 and sklearn/linear_model/tests/test_sag.py : L45 are different than expit() but can be implemented as 1 / expit(). Should it be done that way @ZaydH @rth ?

  10. rth commented on Jan 7, 2019

    @rth
    MemberAuthor

    @RonLek This issue was worked on by @ZaydH and @qdeffense as you can see from the discussion above. It's not ideal that you made a PR for it #12931, ignoring that discussion.

    We will merge that to avoid redundant work, but please don't do that next time.

    @ZaydH @qdeffense There is also a number of other open issues or stalled PRs were your contribution would be appreciated.

  11. RonLek commented on Jan 7, 2019

    @RonLek
    Contributor

    Apologies @rth @qdeffense @ZaydH .
    It was my first time working on such a big repo. I'll keep that in mind. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Easygood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions