scipy.special.log_expit#

scipy.special.log_expit(x, out=None) = <wrapped_ufunc 'log_expit'>#

Logarithm of the logistic sigmoid function.

The SciPy implementation of the logistic sigmoid function is scipy.special.expit, so this function is called log_expit.

The function is mathematically equivalent to log(expit(x)), but is formulated to avoid loss of precision for inputs with large (positive or negative) magnitude.

Parameters:
xarray_like

The values to apply log_expit to element-wise.

outndarray, optional

Optional output array for the function results

Returns:
outscalar or ndarray

The computed values, an ndarray of the same shape as x.

See also

expit

Notes

As a ufunc, log_expit takes a number of optional keyword arguments. For more information see ufuncs

Added in version 1.8.0.

Array API Standard Support

log_expit has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

✅

n/a

CuPy

n/a

✅

PyTorch

✅

⛔

JAX

✅

⛔

Dask

✅

n/a

For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support out, but typically none of the other ufunc kwargs. out is typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend. out is never supported for JAX because JAX arrays are immutable. log_expit does not currently support out for the PyTorch backend.

See Array API coverage for more information.

Examples

>>> import numpy as np
>>> from scipy.special import log_expit, expit
>>> log_expit([-3.0, 0.25, 2.5, 5.0])
array([-3.04858735, -0.57593942, -0.07888973, -0.00671535])

Large negative values:

>>> log_expit([-100, -500, -1000])
array([ -100.,  -500., -1000.])

Note that expit(-1000) returns 0, so the naive implementation log(expit(-1000)) return -inf.

Large positive values:

>>> log_expit([29, 120, 400])
array([-2.54366565e-013, -7.66764807e-053, -1.91516960e-174])

Compare that to the naive implementation:

>>> np.log(expit([29, 120, 400]))
array([-2.54463117e-13,  0.00000000e+00,  0.00000000e+00])

The first value is accurate to only 3 digits, and the larger inputs lose all precision and return 0.