numpy.power(arr1,arr2,out = None,where = True,cast ='same_kind',order ='K',dtype = None):
来自第一个数组的数组元素被提升为来自第二个元素的元素的幂(所有情况都逐个元素发生)。 arr1和arr2必须具有相同的形状,并且arr1中的每个元素都必须从arr2升高到相应的+ ve值;否则将引发ValueError。
参数:
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.
返回:
An array with elements of arr1 raised to exponents in arr2
代码1:将arr1提升为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)
输出:
arr1 : [2, 2, 2, 2, 2] arr2 : [2, 3, 4, 5, 6] Output array : [ 4 8 16 32 64]
代码2:将arr1的元素提高到指数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)
输出:
arr1 : [0 1 2 3 4 5 6 7] Output array : [ 0 1 4 9 16 25 36 49]
代码3:如果arr2具有-ve元素,则错误
# 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)
输出:
arr1 : [2, 2, 2, 2, 2] arr2 : [2, -3, 4, -5, 6] ValueError: Integers to negative integer powers are not allowed.
参考文献:
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.power.html#numpy.power
。
相关用法
注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 numpy.power() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。