What is Numpy exp2() Function

Numpy exp2() is a built-in library function used to find the values 2**x(2 to the power x) or 2^x values for all x belonging to the input array.

NumPy exp2() is a mathematical function that helps the user to calculate 2**x for all x being the array elements.

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(input_array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)


The exp2() function takes one required parameter, the input array, and all the other parameters are optional.

The first parameter is the input array, for which we have to find the exponential values.

The second parameter is the output array which is placed with the result.

The third parameter is where it is used to broadcast over the input values. The fourth and last parameter is the **kwargs, which allows us to pass the variable length keyword to a function’s argument.


Write a program to show the working of the exp2() function in Python.

# app.py

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


python3 app.py
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.

Write a program to show the graphical representation of the exp2() function using a line graph.

See the following code.

# app.py

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.

That’s it for this tutorial.

Leave a Comment

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