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

Overview

The numpy.logaddexp() function in Python is simply used to return the logarithm of the sum of exponentiations of x1 and x2 inputs passed to it.

Mathematically, it’s represented as follows:

numpy.logaddexp(x1, x2) = logarithm(exp(x1) + exp(x2))

Syntax

numpy.logaddexp(x1, x2, out)

Parameter value

The numpy.logaddexp() function takes the following parameter values.

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

Return value

The numpy.logaddexp() function returns the logarithm of the sum of the exponentiations of x1 and x2.

Code example

import numpy as np
# creating our input values
x = 2
y = 3
# calling the logaddexp() function
myresult = np.logaddexp(x, y)
print(myresult)

Explanation

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

Application

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

Free Resources