numpy.power(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) :
Array element from first array is raised to the power of element from second element(all happens element-wise). Both arr1 and arr2 must have same shape and each element in arr1 must be raised to corresponding +ve value from arr2; otherwise it will raise a
ValueError.
Parameters :
arr1 : [array_like]Input array or object which works as base.
arr2 : [array_like]Input array or object which works as exponent.
out : [ndarray, optional]Output array with same dimensions as Input array,
placed with result.
**kwargs : Allows you to pass keyword variable length of argument to a function.
It is used when we want to handle named argument in a function.
where : [array_like, optional]True value means to calculate the universal
functions(ufunc) at that position, False value means to leave the
value in the output alone.
Return :
An array with elements of arr1 raised to exponents in arr2
Code 1 : arr1 raised to arr2
Python
# Python program explaining
# power() function
import numpy as np
# input_array
arr1 = [2, 2, 2, 2, 2]
arr2 = [2, 3, 4, 5, 6]
print ("arr1 : ", arr1)
print ("arr1 : ", arr2)
# output_array
out = np.power(arr1, arr2)
print ("\nOutput array : ", out)
Output :
arr1 : [2, 2, 2, 2, 2]
arr2 : [2, 3, 4, 5, 6]
Output array : [ 4 8 16 32 64]
Code 2 : elements of arr1 raised to exponent 2
Python
# Python program explaining
# power() function
import numpy as np
# input_array
arr1 = np.arange(8)
exponent = 2
print ("arr1 : ", arr1)
# output_array
out = np.power(arr1, exponent)
print ("\nOutput array : ", out)
Output :
arr1 : [0 1 2 3 4 5 6 7]
Output array : [ 0 1 4 9 16 25 36 49]
Code 3 : Error if arr2 has -ve elements
Python
# Python program explaining
# power() function
import numpy as np
# input_array
arr1 = [2, 2, 2, 2, 2]
arr2 = [2, -3, 4, -5, 6]
print ("arr1 : ", arr1)
print ("arr2 : ", arr2)
# output_array
out = np.power(arr1, arr2)
print ("\nOutput array : ", out)
Output :
arr1 : [2, 2, 2, 2, 2]
arr2 : [2, -3, 4, -5, 6]
ValueError: Integers to negative integer powers are not allowed.
References :
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.power.html#numpy.power
.
Similar Reads
numpy.float_power() in Python numpy.float_power(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is raised to the power of element from second element(all happens element-wise). Both arr1 and arr2 must have same shape. float_power differs from the power func
3 min read
numpy.random.power() in Python With the help of numpy.random.power() method, we can get the random samples from power distribution and return the random samples by using this method. power distribution Syntax : numpy.random.power(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can
1 min read
math.pow() in Python In Python, math.pow() is a function that helps you calculate the power of a number. It takes two numbers as input: the base and the exponent. It returns the base raised to the power of the exponent.In simple terms, if you want to find "x raised to the power y", you can use math.pow(x, y).Example:Pyt
3 min read
numpy.exp() in Python numpy.exp(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate exponential of all the elements in the input array. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray
4 min read
numpy.exp2() in Python numpy.exp2(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate 2**x for all x being the array elements. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray, optional
2 min read