np.expm1: What is Numpy expm1() Method in Python
The numpy module has several exponential functions, such as exp, exp2, and expm1, which calculate the exponential values of the elements present inside a numpy array.
np.expm1
The np.expm1() is a numpy library method that returns an exponential value -1 of each element provided inside a numpy array as the output. The np.expm1() function accepts arr_name and out arguments and returns the ndarray of outputs.
Syntax
numpy.expm1 (arr_name, out = None)
Arguments
The numpy expm1() function takes up to two main parameters:
- arr_name: This is the input array in which the elements are supplied.
- out: a ndarray output array where the calculation result is stored elementwise. The shape of the output array is the same as the input array.
Return Value
The numpy expm1() function returns a ndarray of outputs. The output array consists of elementwise exponential value -1 corresponding to each input element.
E.g.: np.expm1(x) will result in:
Output value(x) = Exponential value(x) – 1
Programming Example
Program to show the working of numpy.expm1()
# importing the numpy module import numpy as np # Input array of non-negative integers inp_arr = np.array([1, 2, 5, 6, 8]) # Calculating exponential value of each element inside inp_arr exp_val = np.exp(inp_arr) print("Exponential value of each element is: ", exp_val) # Subtracting 1 from each element of exp_val res_arr = exp_val - 1 # concept of broadcasting is used here print("\nResultant array is: ", res_arr) # Using np.expm1() method to get output_array out_arr = np.expm1(inp_arr) print("\nThe result obtained in output array is: ", out_arr)
Output
Exponential value of each element is: [2.71828183e+00 7.38905610e+00 1.48413159e+02 4.03428793e+02 2.98095799e+03] Resultant array is: [1.71828183e+00 6.38905610e+00 1.47413159e+02 4.02428793e+02 2.97995799e+03] The result obtained in output array is: [1.71828183e+00 6.38905610e+00 1.47413159e+02 4.02428793e+02 2.97995799e+03]
Explanation
In the program expm1.py, we have taken a numpy array named inp_arr and have stored multiple non-negative elements inside the array on which the calculation needs to be performed.
Then, we have passed the complete array as a parameter inside the np.exp() method, which calculates the exponential value of each element inside the inp_arr.
We have subtracted 1 from each element of the exp_val array and stored its result in a resultant array named res_arr, and then its values are printed.
Now, using the np.expm1() method, we have performed a calculation on inp_arr array elements. The output obtained after completing the calculation is stored in the out_arr array, and then the values are printed.
Thus, after comparing the results obtained from res_arr and out_arr, it can be verified that the output received after using np.expm1() is equal to the exponential value of each element – 1.
Program to plot a scatter plot of numpy.expm1() method using the matplotlib module.
# app.py # importing the numpy module import numpy as np # importing the matplotlib module import matplotlib.pyplot as plt # Input array of non-negative integers inp_arr = np.array([1, 2, 5, 6, 8]) # Using np.expm1() method to get output_array out_arr = np.expm1(inp_arr) print("\nThe result obtained in output array is: ", out_arr) plt.style.use('seaborn') plt.plot() plt.plot(inp_arr, out_arr, color='red', marker='^', label='exponential value - 1') plt.title('Use of np.expm1() method') plt.ylabel('Output values') plt.xlabel('Input values') plt.legend() plt.show()
Output
The result obtained in output array is: [1.71828183e+00 6.38905610e+00 1.47413159e+02 4.02428793e+02 2.97995799e+03]
That’s it for this tutorial.