Numpy.exp2() Method Example | Numpy.exp2() Function in Python

NumPy exp2() function is used to find the values 2**x(2 to the power x) or 2^x values for all x belonging to the input array. The exp2() function is defined under numpy, which can be imported as import numpy as np, and we can create multidimensional arrays and derive other mathematical statistics.


numpy.exp2(x, /, out=None, *, where=True)

Parameter values

The numpy.exp2() function takes the following parameter values:

  • x: The array containing the input values.
  • out: A location where the result is stored. This is an optional parameter value.
  • where: The condition over which the input is broadcast. The resulting array will be set to the ufunc result at a given location where this condition is True. Otherwise, the resulting array will retain its original value. This is also an optional parameter value.
  • **kwargs: The other keyword arguments.

Return type

The numpy.exp2() function returns an array holding the result for each of the elements.

Example 1: How to Use np.exp2() Method

import numpy as np

a = [1, 2, 3, 4]
b = [53, 22, 11]

print("Input array: ", a, "\n")
print("2 to the power x values : ", np.exp2(a), "\n")

print("Input array: ", b, "\n")
print("2 to the power x values : ", np.exp2(b), "\n")


Input array: [1, 2, 3, 4]

2 to the power x values : [ 2. 4. 8. 16.]

Input array: [53, 22, 11]

2 to the power x values : [9.00719925e+15 4.19430400e+06 2.04800000e+03]

In this example, we’ve seen that by passing an input array, we get an output array consisting of 2**x values.

Example 2: Plot the np.exp2() Method

import numpy as np
import matplotlib.pyplot as plt

a = [1, 1.5, 2.0, 2.5, 3, 3.5]
b = np.exp2(a)
y = [1, 2, 3, 4, 5, 6]

plt.plot(b, y, color='black', marker="o")


Python NumPy exp2()

The above figure shows the curve of exp2() values of an input array concerning the axes.

Example 3: Numpy exp2() method with Single Number

import numpy as np

print("2**3 is :", np.exp2(3))

print("2**5 is :", np.exp2(5))

print("2**-11 is :", np.exp2(-11))

print("2**(-2) is :", np.exp2(-2))


2**3 is : 8.0
2**5 is : 32.0
2**-11 is : 0.00048828125
2**(-2) is : 0.25

Thanks for reading !!!

#python #numpy

Numpy.exp2() Method Example | Numpy.exp2() Function in Python
1.45 GEEK