Power array elements of an array with a given value and display the result in a different type in Numpy

To power array elements of an array with a given value, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. The dtype parameter is used to set the output datatype.

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 exponent −

p = 2.5

To power array elements of an array with a given value, use the numpy.power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. The dtype parameter is used to set the output datatype −

print("Result...",np.power(arr, p, dtype = complex))


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 exponent
p = 2.5

# To power array elements of an array with a given value, use the numpy.power() method in Python
# Here, the 1st parameter is the base and the 2nd exponents
# The dtype parameter is used to set the output datatype
print("Result...",np.power(arr, p, dtype = complex))

Output

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

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 55.90169944+0.j 316.22776602+0.j 3125. +0.j
4929.50301755+0.j 10119.28851254+0.j 17677.66952966+0.j]