There are many numpy functions that allow you to do mathematical calculations efficiently. The numpy exp() function is one of the function in numpy library. It allows you to calculate the exponential value of all the elements present in the array. This function generally takes four parameters. The first one is your input array, the second is out, the third is where and the last is the dtype that represents the type of the elements. The function returns the exponential values of all the elements present in the input array.
In this article, you will know how to use the numpy exponential function numpy.exp() to calculate numpy exponential values on both 1d and 2d numpy arrays.
Syntax of the Numpy exp() function
Below is the syntax of the numpy.exp() mathematical function. You can read more about it on the official Numpy website.
numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Below is the explanation of the parameters
x: It is your input values of the array
out: The array where you want to store the values. It must be of the same shape as the input array.
where: It is optional.
type: The type of the array that will be returned and it is optional.
The exponential function returns an array with the exponential value of each element.
Quick Implementation of the numpy exponential function
Before going in-depth about the exp() function implementation. Let’s run the quick code on it.
Run the below lines of code to get the exponential values of each element of the numpy array.
import numpy as np scalar_value=10 result = np.exp(10) print(result, '\n1D Array\n') array_1d = np.array([10, 20, 30]) result = np.exp(array_1d) print(result,'\n2D Array\n') array_2d = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) result = np.exp(array_2d) print(result)
Apply np.exp() function on a single or scalar value
Let’s apply np.exp() function on single or scalar value. Here you will use numpy exp and pass the single element to it.
Use the below lines of Python code to find the exponential value of the array.
import numpy as np scalar_value=10 result = np.exp(10) print(result)
Find the Exponential Values of Multiple Elements of 1-D Array
You can also find the exponential of all the elements of a 1D Numpy array. Just pass the whole array as an argument to the exp() function.
Execute the below lines of code to achieve that.
array_1d = np.array([10, 20, 30]) result = np.exp(array_1d) print(result)
[2.20264658e+04 4.85165195e+08 1.06864746e+13]
Find the Exponential Values of Multiple Elements of 2-D Array
In the same way, you can also find the exponential values of a multi-dimensional array. Here you will also use numpy exponential function numpy.exp() function is used to calculate the exponential value of the single element.
Use the below lines of python code to achieve that.
array_2d = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) result = np.exp(array_2d) print(result)
[[2.20264658e+04 4.85165195e+08 1.06864746e+13] [2.35385267e+17 5.18470553e+21 1.14200739e+26] [2.51543867e+30 5.54062238e+34 1.22040329e+39]]
Plot the graph for both array Original and Exponential Array
You can also notice the differences between the original and exponential arrays. To plot the graph you will use the matplot library.
Run the below lines of code to plot the graph.
import numpy as np import matplotlib.pyplot as plt array_1d = np.array([10, 20, 30]) result = np.exp(array_1d) plt.plot(array_1d, result, color = 'red', marker = "*") plt.title("numpy.exp()") plt.xlabel("X") plt.ylabel("Y") plt.show()
That’s all for now. In this article, you have learned the syntax of the exp() function. You have learned how to use this function on scaler, 1D, and 2D numpy array to find the exponential value of every element.
I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.