The Wayback Machine - https://web.archive.org/web/20240629075712/https://www.geeksforgeeks.org/numpy-power-python/
Open In App

numpy.power() in Python

Last Updated : 29 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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 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 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 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 MaskedArray.power() function | Python
numpy.MaskedArray.power() function is used to compute element-wise base array raised to power from second array. It raise each base in arr1 to the positionally-corresponding power in arr2. arr1 and arr2 must be broadcastable to the same shape. Note that an integer type raised to a negative integer power will raise a ValueError. Syntax : numpy.ma.po
2 min read
Raise a square matrix to the power n in Linear Algebra using NumPy in Python
In this article, we will discuss how to raise a square matrix to the power n in the Linear Algebra in Python. The numpy.linalg.matrix_power() method is used to raise a square matrix to the power n. It will take two parameters, The 1st parameter is an input matrix that is created using a NumPy array and the 2nd parameter is the exponent n, which ref
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. Syntax : numpy.random.power(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see that by using numpy.random.power() method, we are able to get the
1 min read
Raise Chebyshev series to a power using NumPy in Python
In this article, we will cover how to raise a Chebyshev series to power in Python using NumPy. polynomial.chebyshev.chebpow method The Chebyshev polynomials are like the other classical orthogonal polynomials which can be defined from a variety of starting locations. Here, we will see how to raise a Chebyshev series to power in Python. The NumPy pa
3 min read
Raise a Hermite_e series to a power using NumPy in Python
In this article, we will cover how to raise a Hermite_e series to power in Python using NumPy. Example Input: [1,2,3,4,5] Output: [3.66165553e+08 1.13477694e+09 3.40357434e+09 3.15464455e+09 4.62071689e+09 2.26002499e+09 2.15384684e+09 6.43945096e+08 4.48843239e+08 8.58196000e+07 4.66275200e+07 5.62768800e+06 2.48058600e+06 1.73880000e+05 6.3900000
2 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
three90RightbarBannerImg