python中power的用法_Python numpy.float_power()用法及代码示例

numpy.float_power(arr1,arr2,out = None,其中= True,强制转换=“ same_kind”,order =“ K”,dtype = None):

来自第一个数组的数组元素被提升为来自第二个元素的元素的幂(所有情况都逐个元素发生)。 arr1和arr2必须具有相同的形状。

float_power与幂函数的不同之处在于,将整数float16和float32提升为float64的最小精度为float64,因此结果始终是不精确的。此函数将为负幂返回可用结果,而对于+ ve幂则很少溢出。

参数:

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

# float_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.float_power(arr1, arr2)

print ("\nOutput array : ", out)

输出:

arr1 : [2, 2, 2, 2, 2]

arr1 : [2, 3, 4, 5, 6]

Output array : [ 4. 8. 16. 32. 64.]

代码2:将arr1的元素提高到指数2

# Python program explaining

# float_power() function

import numpy as np

# input_array

arr1 = np.arange(8)

exponent = 2

print ("arr1         : ", arr1)

# output_array

out = np.float_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元素,则float_power处理结果

# Python program explaining

# float_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.float_power(arr1, arr2)

print ("\nOutput array : ", out)

输出:

arr1 : [2, 2, 2, 2, 2]

arr2 : [2, -3, 4, -5, 6]

Output array : [ 4.00000000e+00 1.25000000e-01 1.60000000e+01

3.12500000e-02 6.40000000e+01]

你可能感兴趣的:(python中power的用法)