Use scipy.special.expit for the inverse of the logit function
See original GitHub issueIn 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:
- Created 5 years ago
- Reactions:1
- Comments:10 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Apologies @rth @qdeffense @ZaydH . It was my first time working on such a big repo. I’ll keep that in mind. Thank you.