Raise the bases to different exponents in Numpy


To raise the bases to different exponents, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents.

Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. An integer type raised to a negative integer power will raise a ValueError. Negative values raised to a non-integral value will return nan. To get complex results, cast the input to complex, or specify the dtype to be complex.

The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

Steps

At first, import the required library −

import numpy as np

Create an array −

arr = np.array([5, 10, 25, 30, 40, 50])

Display the array −

print("Array...
", arr)

Get the type of the array −

print("
Our Array type...
", arr.dtype)

Get the dimensions of the Array −

print("
Our Array Dimension...
",arr.ndim)

Get the shape of the Array −

print("
Our Array Shape...
",arr.shape)

Set the different exponent −

p = [2, 3, 2, 3, 2, 3]

To raise the bases to different exponents, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents −

print("
Result...
",np.power(arr, p))

Example

import numpy as np

# Create an array
arr = np.array([5, 10, 25, 30, 40, 50])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Set the different exponent p = [2, 3, 2, 3, 2, 3] # To raise the bases to different exponents, use the numpy.power() method in Python # Here, the 1st parameter is the base and the 2nd exponents print("
Result...
",np.power(arr, p))

Output

Array...
[ 5 10 25 30 40 50]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 25 1000 625 27000 1600 125000]

Updated on: 08-Feb-2022

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements