# Logarithm of the sum of exponentiations of the inputs in base-2 in Numpy

To get the Logarithm of the sum of exponentiations of the inputs in base 2, use the numpy.logaddexp() method in Python Numpy.

Calculates log2(2**x1 + 2**x2). This function is useful in machine learning when the calculated probabilities of events may be so small as to exceed the range of normal floating-point numbers. In such cases the base-2 logarithm of the calculated probability can be used instead. This function allows adding probabilities stored in such a fashion.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

## Steps

At first, import the required library −

import numpy as np

Log2 input −

one = np.log2(2e-50)
two = np.log2(3.2e-50)

print("Value 1...", one)
print("Value 2...", two)

To get the Logarithm of the sum of exponentiations of the inputs in base 2, use the numpy.logaddexp() method −

res = np.logaddexp(one, two)
print("Logarithm of the sum of exponentiations of the inputs in base 2...",res)

## Example

import numpy as np

# Calculates log2(2**x1 + 2**x2).
# This function is useful in machine learning when the calculated probabilities of events may be so small
# as to exceed the range of normal floating point numbers.
# In such cases the base-2 logarithm of the calculated probability can be used instead.
# This function allows adding probabilities stored in such a fashion.

# Log2 input
one = np.log2(2e-50)
two = np.log2(3.2e-50)

print("Value 1...", one)
print("Value 2...", two)

# To get the Logarithm of the sum of exponentiations of the inputs in base 2, use the numpy.logaddexp() method in Python Numpy
print("Logarithm of the sum of exponentiations of the inputs in base 2...",res)

## Output

Value 1...
-165.09640474436813
Value 2...
-164.41833283925547

Logarithm of the sum of exponentiations of the inputs in base 2...
-164.00781734564688
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}