Numpy Power Tutorial | Numpy.power() | np.power()

Introduction

NumPy is a popular package for scientific computing in Python that has many functions for linear algebra operations on N-dimensional array objects. Numpy.power() is a function that is used to calculate the element-wise exponential or power of the NumPy array. In this tutorial, we’ll see its syntax, usage, and various aspects of the np.power() function along with multiple examples.

Syntax of numpy.power()

The syntax of numpy.power() function is as follows –

numpy.power(x1, x2, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘power’)

Parameters:

  • x1: It denotes the base array-like object.
  • x2: It denotes the exponent array-like object.
  • out: A ndarray into which the result is stored. If not provided or None, a freshly allocated array is returned.
  • Other parameters like where, casting, order, dtype, subok, signature, and extobj are optional and rarely used in standard usage.

Examples of Numpy Power Function | numpy.power()

Import Library

We start by importing the Numpy library as below.

In [0]:

import numpy as np

Example 1: Simple Usage of numpy.power() Function on Scalar

In the first example of np.power() function, we use scaler values as both the base (5)  and exponent (3), the resulting output being 5 to the power 3 or 125, a scalar value.

In [1]:

base = 5
exponent = 3

result = np.power(base, exponent)
print(result)

Out[1]:

125

Example 2: Scalar as Base and 1-D Array as Exponential in np.power()

In this example, we keep the base as scalar and the exponent as NumPy 1-D array. Due to the broadcasting effect, the exponential of the base is calculated for each element of the exponent array. This results in a NumPy 1-D array as output.

In [2]:

base=3
exponent = np.array([2, 3, 4])

result_scalar_base = np.power(base, exponent)
print(result_scalar_base)

Out[2]:

[ 9 27 81]

Example 3: 1-D Array as Base and Scalar as Exponential in np.power()

This time we are using a NumPy 1-D array as the base and a scalar as the exponential. Again due to the broadcasting effect, each element of the base array is raised to the power of scalar value 3. The resulting output is also a 1-D NumPy array.

In [3]:

base = np.array([2, 3, 4])
exponent = 3

result_scalar_exponent = np.power(base, 2)
print(result_scalar_exponent)

Out[3]:

[ 4 9 16]

Example 4: Both Base and Exponent as 1-D Array in numpy.power()

In this example, we are using two NumPy 1-D array as base and exponent. The exponential of base is calculated element-wise between the two arrays.

In [4]:

base = np.array([2, 3, 4])
exponent = np.array([2, 3, 2])

result = np.power(base, exponent)
print(result)

Out[4]:

[ 4 27 16]

Example 5: 2-D Array as Base and Scalar as Exponential

Here we are applying scalar exponential over a 2-D NumPy array. The power is calculated for each element of base 2-D array with the scalar exponential value.

In [5]:

base_2d = np.array([[1, 2, 3],
                    [4, 5, 6]])
exponent_scalar = 2

result_2d = np.power(base_2d, exponent_scalar)
print(result_2d)

Out[5]:

[[ 1  4  9]
 [16 25 36]]

Example 6: Both Base and Exponent as 2-D Array

In this example, we use 2-D NumPy arrays as both base and exponential in np.power() function.

In [6]:

base_2d = np.array([[1, 2, 3],
                    [4, 5, 6]])

exponent_2d = np.array([[2, 2, 2],
                        [3, 3, 3]])

result_2d = np.power(base_2d, exponent_2d)
print(result_2d)
Out[6]:
[[  1   4   9]
 [ 64 125 216]]

Example 7: Use of dtype Parameter in np.power()

By default, nump.power() uses the data type of the input arrays. However, you can also specify a datatype using the dtype parameter. In this example, we specified dtype=np.float64, as a result, the output is of type float even though input arrays were of type Int.
In [7]:
base = np.array([1, 2, 3])
exponent = 2

result_float = np.power(base, exponent, dtype=np.float64)
print(result_float)

Out[7]:

[1. 4. 9.]

Example 8: Raising a Negative Number to a Non-Integer Power

In pure mathematics, raising a negative number to a non-integer power isn’t real-valued. In such cases, numpy.power()returns complex numbers as shown in the below example. Please make sure you pass dtype=np.complex128 in np.power() function otherwise it will return nan in the output array.

In [8]:

base = np.array([-2, -3], dtype=np.complex128)
exponent = np.array([0.5, 1.5])

result = np.power(base, exponent)
print(result)
Out[8]:
[ 8.65956056e-17+1.41421356j -9.54517715e-16-5.19615242j]

Example 9: Zero Raised to the Power of Zero in numpy.power()

Mathematically, 0 raised to the power of 0 is indeterminate. However, in numpy.power() it’s defined to be 1 for consistency and practical reasons.

In [9]:

base_zero = np.array([0])
exponent_zero = np.array([0])

result_zero = np.power(base_zero, exponent_zero)
print(result_zero)

Out[9]:

[1]

Example 10: Use of Where Parameter in np.power() 

The where parameter in np.power() allows you to specify where the operation should be applied in the output array. This can be particularly useful when you want to conditionally compute the power operation for certain elements based on a condition or mask.

In this example, the power operation is applied only to the 3rd and 4th elements of the base array (since only they are greater than 2), and the output reflects that. The first two values remain unchanged as 1 (due to our initialization).

In [10]:

base = np.array([1, 2, 3, 4])
exponent = np.array([2, 3, 2, 3])

# Create a boolean mask where base values are greater than 2
condition = base > 2

# Initialize an array with default values (e.g., ones)
result = np.ones_like(base)

np.power(base, exponent, where=condition, out=result)
print(result)

Out[10]:

[ 1 1 9 64]

 

Reference: NumPy Documentation

  • MLK

    MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *