What is the numpy.exp2() Method in Python

The numpy.exp2() method is “used to calculate element-wise 2**x for all x in the input array”.

Syntax

numpy.exp2(input_array, out = None, where = True, **kwargs)

Parameters

  1. input_array: The array containing the input values for which we must find the exponential values.
  2. out: It is the output array that is placed with the result.
  3. where: It is used to broadcast over the input values.
  4. **kwargs: It allows us to pass the variable length keyword to a function’s argument.

Return value

The numpy.exp2() method returns an array holding the result for each element.

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")

Output

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")
plt.title("numpy.exp2()")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output

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))

Output

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

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.