How to Use NumPy Exponential Function

  • Post author:
  • Post category:NumPy
  • Post last modified:August 11, 2022

NumPy exp() in Python is a mathematical function used to calculate the exponential values of all the elements present in the input array. This function takes four arguments which are array, out, where, dtype, and returns an array containing all the exponential values of the input array.

In this article, I will explain syntax and how to use the numpy.exp() function on single and multi-dimension arrays.

1. Quick Examples of NumPy Exponential Function

If you are in a hurry, below are some quick examples of how to use the NumPy exponential function.


# Below are the quick examples

# Example 1: Get the exponential Value of single element
arr = np.exp(3)

# Example 2: Get the exponential values of multiple elements of 1-d array 
arr = [2, 5, 8]
arr2 = np.exp(arr)

# Example 3: Get the exponential values of 2-D numpy array elements
arr = np.array([[4, 6, 3, 7], [8, 5, 2, 9]])
arr2 = np.exp(arr)

# Example 4: Use numpy.exp() function to graphical representation  
arr = [1, 1.4, 1.8, 2, 2.6, 3]
out_array = np.exp(arr)
arr2 = [1, 1.3, 1.6, 2.3, 2.8, 3]
plt.plot(arr, arr2, color = 'green', marker = "*") 

# Yellow for numpy.exp()
plt.plot(out_array, arr2, color = 'yellow', marker = "o")
plt.title("numpy.exp()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show() 

2. Syntax of numpy.exp()

Following is the syntax of the numpy.exp() function.


#Syntax of numpy.exp()
numpy.exp(arr, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)

2.1 Parameters of numpy.exp()

  • arr : Input array.
  • out : An array where the result is stored. When provided, it must have the shape of the inputs.
  • where: optional
  • dtype – Type of the returned array and it is optional.

2.2 Return Value of numpy.exp()

This function returns an array containing all the exponential values of all elements of the input array.

3. Use NumPy exp() to get Exponential Value

This mathematical Python NumPy exp() function is used to calculate the exponential values of all the elements present in the input array.


import numpy as np

# get the exponential Value of single element
arr = np.exp(3)
print(arr)

# Output
# 20.085536923187668

3.2 Get the Exponential Values of Multiple Elements of 1-D Array

To calculate the exponential values of integer array elements by using the numpy.exp() function. For example,


# Create an 1D input array  
arr = [2, 5, 8]

# Get the exponential values of multiple elements of 1-d array 
arr2 = np.exp(arr)
print (arr2)

# Output
# [   7.3890561   148.4131591  2980.95798704]

4. Get the Exponential Values of 2-D NumPy Array Elements

Let’s use a 2-Dimensional array and get the exponential values for all elements in the array. Let’s create a 2-D NumPy array using numpy.array().


# creating an 2D input array
arr = np.array([[4, 6, 3, 7], [8, 5, 2, 9]])

# get the exponential values of 2-D numpy array elements
arr2 = np.exp(arr)
print(arr2)

# Output
# [[5.45981500e+01 4.03428793e+02 2.00855369e+01 1.09663316e+03]
#  [2.98095799e+03 1.48413159e+02 7.38905610e+00 8.10308393e+03]]

5. Use numpy.exp() Function to Graphical Representation

We can use NumPy exp() function and represent the value graphically using the MatLab library.


import numpy as np
import matplotlib.pyplot as plt

# Use numpy.exp() function to graphical representation  
arr = [1, 1.4, 1.8, 2, 2.6, 3]
out_array = np.exp(arr)
arr2 = [1, 1.3, 1.6, 2.3, 2.8, 3]
plt.plot(arr, arr2, color = 'green', marker = "*") 
 
# yellow for numpy.exp()
plt.plot(out_array, arr2, color = 'yellow', marker = "o")
plt.title("numpy.exp()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show() 

Yields below output.

NumPy exponential

You can see the Parabolic graph of the exp() function in Numpy.

6. Conclusion

In this article, I have explained how to use Python numpy.exp() function and how to calculate the exponential value of every element in the given array with examples by using 1-D and 2-D arrays.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing How to Use NumPy Exponential Function