NumPy exp() Function:
Numpy.exp: To determine e raised to the power of a given value(ex), use the exp() function of the Numpy module. Here, e is the natural logarithm’s base, and its value is about 2.718282.
Syntax:
numpy.exp(a, out=None)
Parameters
a: This is required. It is the array or an object given as input.
out: This is optional. It is the location where the result will be stored. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
Numpy exp: The exponential value of each element of the given array(a) is returned by the exp() function of the NumPy module.
NumPy exp() Function in Python
Example1
Approach:
- Import numpy module using the import keyword
- Pass some random list as an argument to the array() function of the numpy module to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array as an argument to the exp() function of the numpy module to get the exponential(ex) values of each element of the given array.
- Store it in another variable.
- Print the exponential values of each element of the given array
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function # of the Numpy module to create an array. # Store it in a variable. gvn_arry = np.array([3, 6, 4, 10, 1]) # Print the above given array. print("The above given array is:") print(gvn_arry) print() # Pass the above given array as an argument to the exp() function of the # numpy module to get the exponential values of each element of # the given array # Store it in another variable. rslt = np.exp(gvn_arry) # Print the exponential values of each element of the given array print("The exponential values of each element of the given array:") print(rslt)
Output:
The above given array is: [ 3 6 4 10 1] The exponential values of each element of the given array: [2.00855369e+01 4.03428793e+02 5.45981500e+01 2.20264658e+04 2.71828183e+00]
Example2(Plotting a Graph)
Approach:
- Import numpy module using the import keyword
- Import pyplot from the matplotlib module using the import keyword
- Give some random list as static input and store it in a variable.
- Pass the above-given list as an argument to the exp() function of the numpy module to get the exponential(ex) values of each element of the given array.
- Store it in another variable.
- Store the above input array in another variable for plotting the input array vs input array.
- Plot the input array versus input array with some random color and marker values using the plot() function of the matplotlib module
- Plot the output array(exponential values) versus input array with some other random color and marker values using the plot function of the matplotlib module
- Give the title of the plot using the title() function of the matplotlib module
- Display the plot using the show() function of the matplotlib module.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Import pyplot from the matplotlib module using the import keyword import matplotlib.pyplot as plt # Give some random list as static input and store it in a variable. gvn_arry = [1.3, 1, 2.4, 3.2, 4] # Pass the above-given list as an argument to the exp() function of the numpy module to # get the exponential(ex) values of each element of the given array. # Store it in another variable. rslt_arry = np.exp(gvn_arry) # Store the above input array in another variable for plotting the input array vs input array. temp_inputarry = [1.3, 1, 2.4, 3.2, 4] # Plot the input array versus input array with some random color and marker values using # the plot() function of the matplotlib module plt.plot(gvn_arry, temp_inputarry, color = 'green', marker = "*") # Plot the output array(exponential values) versus input array with some other random color # and marker values using the plot function of the matplotlib module plt.plot(rslt_arry, temp_inputarry, color = 'orange', marker = "o") # Give the title of the plot using the title() function of the matplotlib module plt.title("Exponential values") plt.xlabel("X") plt.ylabel("Y") # Display the plot using the show() function of the matplotlib module. plt.show()
Output: