question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/scikit-learn/scikit-learn/pull/12909?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
ZaydHcommented, Jan 3, 2019

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.

1reaction
RonLekcommented, Jan 7, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.special.expit — SciPy v0.14.0 Reference Guide
The expit function, also known as the logistic function, is defined as expit(x) = 1/(1+exp(-x)). It is the inverse of the logit function....
Read more >
scipy.special.expit() - JAX documentation
The expit function, also known as the logistic sigmoid function, is defined as expit(x) = 1/(1+exp(-x)) . It is the inverse of the...
Read more >
python - logit and inverse logit functions for extreme values
About the reason your functions wore better with negative values. ... from scipy.special import logit, expit >>> import numpy as np ...
Read more >
scipy.special.expit — SciPy v0.14.0 Reference Guide
The expit function, also known as the logistic function, is defined as expit(x) = 1/(1+exp(-x)). It is the inverse of the logit function....
Read more >
Python Logistic Regression - Ajay Tech
The inverse of the logit curve is the inverse-logit or sigmoid function ( or expit function as sklearn calls it). ... from scipy.special...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found