Built with Tutify.io

    AIStudyLabs

    How to Use NumPy Exponential Function

    How to Use NumPy Exponential Function

    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.

    <a id="h-1-quick-examples-of-numpy-exponential-function"></a>

    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()
    

    <a id="h-2-syntax-of-numpy-exp"></a>

    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)
    

    <a id="h-2-1-parameters-of-numpy-exp"></a>

    2.1 Parameters of numpy.exp()

    <a id="h-2-2-return-value-of-numpy-exp"></a>

    2.2 Return Value of numpy.exp()

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

    <a id="h-3-use-numpy-exp-to-exponential-of-a-single-value"></a>

    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.

    <a id="h-3-2-get-the-exponential-values-of-multiple-elements-of-1-d-array"></a>

    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.

    <a id="h-4-get-the-exponential-values-of-2-d-numpy-array-elements"></a>

    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]]
    

    <a id="h-5-use-numpy-exp-function-to-graphical-representation"></a>

    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.

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

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

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

    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.

    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.

    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.

    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.

    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.

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References