What is the numpy.logaddexp2() function from NumPy in Python?

Overview

The function logaddexp() of numpy in Python returns the logarithm of the sum of exponentiations of x1 and x2 inputs in base-2.

It can be expressed mathematically as:

logaddexp(x1, x2) = log2(2x12^{x1} + 2x22^{x2})

Syntax

logaddexp2(x1, x2, out)

Parameters

The logaddexp2() function takes the following parameter values:

  • x1, x2: This represents the input arrays or scalers.
  • out: This represents a location where the result is stored. This is optional.

Return value

The logaddexp2() function returns the logarithm of the sum of the exponentiations of x1 and x2 in base-2.

Example

from numpy import logaddexp2
# creating our input values
x = 2
y = 3
# calling the logaddexp2() function
myresult = logaddexp2(x, y)
print(myresult)

Explanation

  • Line 1: We import the logaddexp2 from numpy module.
  • Lines 3 and 4: We create variables x and y.
  • Line 7: We call the logaddexp2() function on the variables x and y. The result is assigned to a variable myresult.
  • Line 9: We print the variable myresult.

Application

The function logaddexp2() is useful in statistics, especially when the probability of an event is so tiny that it exceeds the range of regular floating-point numbers. In such cases, the logarithm of the calculated probability is stored. The function allows storing the added probabilities in such a fashion.