Built with Tutify.io

    AIStudyLabs

    NumPy power() Function in Python

    NumPy power() Function in Python

    Python NumPy power() function is used to compute the first array of elements raised to powers from the second array of elements, element-wise. Both arrays must be having the same shape and each element of the first array must be raised to the corresponding positive value from the second array. If both arrays are not of the same size or the second array has negative values, this function returns a value Error.

    In this article, I will explain the NumPy power() method syntax, parameters, and usage of how to compute the element-wise exponentiation of an input array. It raises each element of the input array to a specified power.

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

    1. Quick Examples of power() Function

    If you are in a hurry, below are some quick examples of how to use the power() function.

    
    # Quick examples of power() function
    
    # Example 1: Get the scalar as exponent of an array 
    arr = np.power(4,3)
    
    # Example 2: Get the exponents of an array 
    arr = np.array([2, 3, 5, 8, 9,4])
    arr1 = np.power(arr, 3)
    
    # Example 3: Get the exponents of two 1-Darray
    arr = [2, 4, 6, 5, 3]
    arr1 = [2, 3, 5, 4, 1]
    arr2 = np.power(arr,arr1)
    
    # Example 4: Use negative exponent 
    # With numpy.Power() function
    arr = [2, 4, 6, 5, 3]
    arr1 = [-2, 3, -5, 4, 1]
    arr2 = np.power(arr,arr1)
    
    # Example 5: Get the power vales of 2-D array
    # Using numpy.power() function
    arr = np.array([[4,2,3],[3,2,5]])
    arr1 =np.array([[3,2,3],[1,2,3]])
    arr2 = np.power(arr,arr1)
    
    # Example 6: Get the power values 
    # Using ** operator
    arr = np.array([[4,2,3],[3,2,5]])
    arr1 =np.array([[3,2,3],[1,2,3]])
    arr2 = arr**arr1
    

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

    2. Syntax of NumPy power() function

    Following is the syntax of numpy.power() function.

    
    # Syntax of numpy.power() 
    numpy.power(arr, arr1, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None)
    

    <a id="h-2-1-parameter-of-power"></a>

    2.1 Parameter of power()

    Following are the parameters of the power() function.

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

    2.2 Return Value

    It returns an array with elements of the first array raised to exponents in the second array(element-wise). It returns ValueError if the second array elements are negative. It returns scalar if both the first and second arrays have scalar elements.

    <a id="h-3-usage-of-numpy-power-function"></a>

    3. Usage of NumPy power() Function

    The np.power() is a mathematical library function that is used to return an array containing elements of the first array raised to the power element of the second array. The NumPy power() function refers to elements in the first array as a base and returns it to the power of the corresponding component of the second array.

    <a id="h-3-1-get-the-scalar-power-value"></a>

    3.1 Get the Scalar Power Value

    You can get the scalar value as a power value of given elements by passing the elements as parameters to this function. Here, the first element is the base and the second element is the exponent. The following example computes 4 the 3rd power which returns 64.

    
    # Import numpy
    import numpy as np
      
    # Get the scalar as exponent of an array 
    arr = np.power(4,3)                
    print("Power value:", arr)
    

    Yields below output.

    <a id="h-4-get-the-power-for-1-d-array"></a>

    4. Get the Power for 1-D Array

    If you want to calculate the element-wise power of a 1-dimensional NumPy array, you can use the numpy.power() function.

    In the below example, the np.power(arr, 3) function raises each element of the 1-dimensional array arr to the power of 3. Each element of the original array is squared element-wise using the np.power() function. You can replace 3 with any other exponent value to calculate the element-wise power for the 1-dimensional array.

    
    # Create a 1-D array
    arr = np.array([2, 3, 5, 8, 9, 4])
    print("Original array:\n", arr)
    
    # Get the exponents of an array
    arr1 = np.power(arr, 3)
    print("After getting the element-wise power calculation:\n", arr1)
    

    Yields below output.

    You can get exponents of two 1- D NumPy arrays element-wise. Here the first array ‘arr’ elements act as a base, and the second array ‘arr1’ elements act as an exponent. This function will return an array with elements of the first array raised to exponents in the second array(element-wise)

    
    # Get the exponents of two 1-Darray
    arr = [2, 4, 6, 5, 3]
    arr1 = [2, 3, 5, 4, 1]
    arr2 = np.power(arr,arr1)
    print("After getting the element-wise power calculation:\n", arr2)
    
    # Output:
    # After getting the element-wise power calculation:
    #  [   4   64 7776  625    3]
    

    <a id="h-5-use-negative-exponent-with-power-function"></a>

    5. Use Negative Exponent With power() Function

    The numpy.power() doesn’t work with negative exponents. If you try to use the negative power, it will throw ValueError Integers to negative integer powers are not allowed.

    
    # Use negative exponent
    # With numpy Power() function
    arr = [2, 4, 6, 5, 3]
    arr1 = [-2, 3, -5, 4, 1]
    arr2 = np.power(arr,arr1)
    print("After element-wise power calculation with negative exponent:",arr1)
    
    # Output:
    # ValueError: Integers to negative integer powers are not allowed.
    

    <a id="h-6-get-the-power-of-2-d-numpy-array"></a>

    6. Get The Power of 2-D NumPy Array

    You can calculate the powers of two-Dimensional NumPy arrays element-wise using power() function. The shape of two 2-D NumPy arrays must be the same shape. Let's check the process.

    In the below example, create two 2-dimensional NumPy arrays arr and arr1, and then use the np.power() function to perform element-wise exponentiation of these arrays. In this case, each element arr is raised to the power of the corresponding element arr1, resulting in the element-wise power values of the 2-dimensional arrays.

    
    # Create 2-D array
    arr = np.array([[4,2,3],[3,2,5]])
    arr1 = np.array([[3,2,3],[1,2,3]])
    
    # Get the power vales of 2-D array
    arr2 = np.power(arr,arr1)
    print("After getting the element-wise power calculation:\n",arr2)
    
    # Output:
    # After getting the element-wise power calculation:
    #  [[ 64   4  27]
    #  [  3   4 125]]
    

    Alternatively, you can also use ** operator and get the power values of an array of elements. Both the np.power() function and the ** operator can be used to calculate the element-wise power of NumPy arrays, providing flexibility and ease of use for mathematical operations.

    
    # Create 2-D array
    arr = np.array([[4,2,3],[3,2,5]])
    arr1 = np.array([[3,2,3],[1,2,3]])
    
    # Get the power values using ** operator
    arr2 = arr**arr1
    print("After getting the element-wise power calculation:\n",arr2)
    
    # Output:
    # After getting the element-wise power calculation:
    #  [[ 64   4  27]
    #  [  3   4 125]]
    

    You can see that the difference between the first and second approaches is execution time. The ****** approach is slightly faster than the np.power() method.

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

    Frequently Asked Questions

    The numpy.power() function is used to compute the element-wise exponentiation of an input array. It raises each element of the input array to a specified power, allowing for efficient element-wise power calculations on NumPy arrays.

    The input arrays must have the same shape for element-wise power calculation. If the arrays have different shapes, a ValueError will be raised.

    If the exponent array contains negative values, the numpy.power() function will still perform the element-wise power calculation, but the result will be in fractional form, as negative exponents represent reciprocals.

    You can use the ** operator for element-wise power calculation on NumPy arrays. For example, arr ** exponent will raise each element of the arr array to the power specified in the exponent array.

    Both numpy.power() and the ** operator provides similar functionality for element-wise power calculation. The choice between them can depend on readability and personal preference. In general, there should be no significant performance difference between the two methods for most use cases.

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

    Conclusion

    In this article, I have explained how to use the NumPy power() method syntax, parameters, and usage of how to get the power values of single-dimensional and multi-dimensional NumPy arrays. The function is used to compute the first array of elements raised to powers from the second array of elements, element-wise with examples.

    Happy Learning!!

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

    Related Articles

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

    References