How to Use Numpy Logaddexp2 in Python?

Numpy Logaddexp2

There are many articles in AskPython detailing the various functions that the numpy library of Python has got to offer. In this article, we shall explore one which combines the logarithmic and exponential functions to the base of 2 – the logaddexp2( ) function. Let’s get things started by first importing the numpy library using the following code.

import numpy as np

Thereafter, we shall explore further the logaddexp2( ) function through each of the following sections.

  • Syntax of the logaddexp2( ) function
  • Why use logaddexp2(a,b) instead of log2(2**a + 2**b)?
  • Using logaddexp2( ) on N-Dimensional Arrays

Syntax of the logaddexp2( ) function

The logaddexp2( ) function adds a pair of 2’s raised to the power of input scalars or arrays and then calculates the natural logarithm of the resulting value again with the base of 2. Following is the syntax of the logaddexp2( ) function which contains both the mandatory and the optional inputs required for its functioning.

numpy.logaddexp2(x1, x2, out=None, *, where=True, dtype=None)

where,

  • x1, x2 – Scalars or N-dimensional arrays which are to be used with base 2
  • out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same length as the output
  • * kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function
  • where – an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculate when set to False
  • dtype – an optional construct used to specify the data type which is being used

Why use logaddexp2(a,b) instead of log2(2**a + 2**b)?

There are independent functions within the numpy library that can help in deducing the required results. It can be done by summing up the inputs raised to the power of ‘2’ & then deducing the logarithm of the sum to the base of 2. So, let’s find out why there’s an exclusive function to perform the same set of operations.

a = 1.03
b = 0.0006
np.log2(2**a + 2**b)
np.logaddexp2(a,b)

Following are the results of the above code.

Comparing Results 1
Comparing the Results

Seems that the results are identical. But, what if we reduce ‘x’ a tad bit lesser and repeat the same? Let’s find out what happens then!

a = 1.03e-5
b = 6e-7
np.log2(2**a + 2**b)
np.logaddexp2(a,b)
Comparing The Results When A B LT 0
Comparing the Results when a,b << 0

One can see that there is an additional decimal after the last digit with the logaddexp2( ) function. This serves as a great utility during machine learning applications which involve minuscule probabilities of events. These probabilities might be very small that they can exceed the normal range of floating point numbers. Since the logaddexp2( ) function uses the base of 2 for the results to be more accurate.


Using logaddexp2( ) on N-Dimensional Arrays

In this section, we shall demonstrate the usage of logaddexp2( ) function only at select positions of an N-dimensional array.

ar1 = np.array([[1e-10, 8e-3, 0.09],
                [5.007, 33e-5, 2e-7]])
ar2 = np.array([[2e-6, 10e-3, 9e-4],
                [0.006, 4e-9, 7e-6]])
np.logaddexp2(ar1, ar2, where = [[False, True, True],
                                 [True, True, False]])
Using Logaddexp2 On N Dimensional Arrays
Using logaddexp2( ) on N-Dimensional Arrays

The usage of where option has made valid results be returned only in the positions given as True & those elsewhere as erroneous. Let’s assign this to an output array to make believe it.

r = np.zeros((2,3))
np.logaddexp2(ar1, ar2, where = [[False, True, True],
                                [True, True, False]], out = r)
Zeros Returned On The False Positions
Zeros Returned On The False Positions

With a keen eye, one can observe that only the values returned by the function remain whilst those that are erroneous are replaced with zeros.


Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the logaddexp2( ) function from the numpy library. Here’s another article that details the usage of logaddexp( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est faucere!

References