AppDividend
Latest Code Tutorials

Numpy exp2: The Complete Overview

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

Numpy exp2() is a built-in library function that 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.

Syntax

numpy.exp2(input_array, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)

Parameters

The exp2() function takes one required parameter, which is 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 for which is placed with the result.

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

Example

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

Output

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

Output

Python NumPy exp2()

In the above figure, we can see the curve of exp2() values of an input array concerning the axes.

That’s it for this tutorial.

See also

Numpy fix()

Numpy ceil()

Numpy floor()

Numpy degrees()

Numpy absolute()

Leave A Reply

Your email address will not be published.

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