How to Use NumPy Exponential Function

  • Post author:
  • Post category:NumPy
  • Post last modified:November 8, 2023
  • Reading time:15 mins read

NumPy exponential 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.


# Quick examples of numpy exponential function

# 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 – The input array. This can be a scalar, a 1-D array, or a multi-dimensional array. The exponential function will be applied element-wise to this array.
  • <strong>out(optional)</strong> – An alternative output array in which to place the result. If provided, it must have the same shape as the input array x. If not provided or None, a new array is created.
  • where (optional) – This parameter allows you to specify a condition where the function is applied. Only the elements where the condition is True will be computed. By default, it is set to True, meaning the function is applied to all elements.
  • casting(optional) – Controls what kind of data casting may occur. Default is ‘same_kind’.
  • order(optional) – Specifies the memory layout of the output array. Default is ‘K’ which means matches the layout of x.
  • dtype(optional) – The desired data type for the output array. If not specified, the data type of the input array is used.

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 Exponential of a Single Value

To calculate the exponential of a single value in Python using NumPy, you can use the np.exp() function. For instance, you’ve imported NumPy and then used the np.exp() function to calculate the exponential value of a single element (in this case, the value 3), and the result is printed, which is approximately 20.085536.


# Import numpy
import numpy as np

# Get the exponential Value 
# Of single element
arr = np.exp(3)
print("Exponential Value:",arr)

Yields below output.

numpy exponential function

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

You can calculate the exponential of each element in a NumPy array using the np.exp() function. For instance, np.exp(arr) calculates the exponential of each element in the NumPy array arr. The exponential values of the elements in the NumPy array are calculated and printed accordingly.


import numpy as np

# Create an 1D input array  
arr = np.array([2, 5, 8])
print("Original array:\n",arr)

# Get the exponential values 
# Of multiple elements of 1-d array 
arr2 = np.exp(arr)
print("Exponential values of 1D numpy array elements:\n",arr2)

Yields below output.

numpy exponential function

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

You can use the np.exp() function to calculate the exponential values of a 2D NumPy array’s elements. For example, np.exp(arr) calculates the exponential of each element in the 2D NumPy array arr.


import numpy as np

# Creating an 2D input array
arr = np.array([[4, 6, 3, 7], [8, 5, 2, 9]])
print("Original array:\n",arr)

# Calculate the exponential values 
# Of 2-D numpy array elements
arr2 = np.exp(arr)
print("Exponential values of 2D numpy array elements:\n",arr2)

# Output:
# Original array:
#  [[4 6 3 7]
#  [8 5 2 9]]
# Exponential values of 2D numpy array elements:
#  [[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.

Frequently Asked Questions

What does the NumPy exponential function do?

The NumPy exponential function, np.exp(), calculates the exponential of each element in a NumPy array. It is useful for exponentiating numerical values efficiently.

Can I calculate the exponential of a single value using NumPy?

You can calculate the exponential of a single value using NumPy’s np.exp() function. For example, np.exp(2) calculates the exponential of the value 2, and the result is approximately 7.389.

What is the default data type of the output when using np.exp()?

The default data type of the output array when using np.exp() is the same as the data type of the input array. You can also specify a different data type using the dtype parameter if needed.

Can I calculate the exponential values of elements in a 2D NumPy array?

You can calculate the exponential values of elements in a 2D NumPy array using the np.exp() function. It works element-wise on multi-dimensional arrays, including 2D arrays.

Does np.exp() work element-wise on multi-dimensional arrays?

The np.exp() works element-wise on multi-dimensional arrays in NumPy. When you apply the np.exp() function to a multi-dimensional array, it calculates the exponential of each element independently, preserving the shape of the input array.

How do I calculate the exponential values of elements in a NumPy array?

You can calculate the exponential values of elements in a NumPy array using the np.exp() function. This function calculates the exponential of each element in the input array.

Conclusion

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

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply