Utilities#
Module with utilities for the mixture model package.
- lymixture.utils.late_binomial(support: ndarray, p: float = 0.5) ndarray[source]#
Parametrized binomial distribution.
- lymixture.utils.map_to_simplex(from_real: ndarray | list[float]) ndarray[source]#
Map from real numbers to simplex.
The result has one entry more than
values. The method creates a simplex by adding a dimension which is fixed to zero Then the values are run through a softmax function to normalize them.>>> real = [4, 3.5] >>> map_to_simplex(real) array([0.01127223, 0.61544283, 0.37328494])
- lymixture.utils.map_to_real(from_simplex: ndarray | list[float]) ndarray[source]#
Map from simplex to real numbers.
>>> simplex = [0.01127223, 0.61544283, 0.37328494] >>> np.allclose(map_to_real(simplex), [4, 3.5]) True
- lymixture.utils.normalize(values: ndarray, axis: int, **isclose_kwargs: float | bool) ndarray[source]#
Normalize
valuesto sum to 1 alongaxis.Beyond normalizing, this function also sets values that are close to zero to the exact value of zero. For this, it passes all extra keyword arguments to numpy’s
isclosefunction.>>> normalize(np.array([0.1, 0.2, 0.7]), axis=0) array([0.1, 0.2, 0.7]) >>> normalize(np.array([1e-20, 0.3, 0.7]), axis=0) array([0. , 0.3, 0.7]) >>> normalize(np.array([1e-20, 0.3, 0.3]), axis=0) array([0. , 0.5, 0.5])
- lymixture.utils.log_normalize(log_values: ndarray, axis: int) ndarray[source]#
Log-normalize
log_valuesto sum to 1 alongaxis.>>> log_norm = log_normalize(np.log([0.1, 0.2, 0.7]), axis=0) >>> np.exp(np.logaddexp.reduce(log_norm, axis=0)) np.float64(1.0)
- lymixture.utils.harden(values: ndarray, axis: int) ndarray[source]#
Harden
valuesto become a one-hot-encoding along the givenaxis.>>> values = np.array( ... [[0.1, 0.2, 0.7], ... [0.3, 0.4, 0.3]] ... ) >>> harden(values, axis=1) array([[0, 0, 1], [0, 1, 0]]) >>> arr = np.array([[[0.84, 0.64, 0.3 , 0.23], ... [0.18, 0.31, 0.23, 0.54], ... [0.08, 0.05, 0.72, 0.09]], ... [[0.33, 0.43, 0.28, 0.54], ... [0.26, 0.48, 0.8 , 0.01], ... [0.45, 0.09, 0.64, 0.11]]]) >>> harden(arr, axis=2) array([[[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], [[0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 0]]]) >>> harden(np.array([0.1, 0.2, 0.3, 0.1]), axis=0) array([0, 0, 1, 0])