numpy.divide(arr1,arr2,out = None,where = True,cast ='same_kind',order ='K',dtype = None):
将第一个数组中的数组元素除以第二个元素中的元素(所有情况均逐个元素发生)。 arr1和arr2必须具有相同的形状,并且arr2中的元素不能为零;否则会引发错误。
参数:
- arr1     : [数组]输入数组或对象,用作除数。
arr2 : [数组]用作除数的输入数组或对象。
out : [ndarray,可选]尺寸与输入数组相同的输出数组,
放在结果上。
** kwargs:允许您将关键字的可变参数长度传递给函数。
当我们要处理函数中的命名参数时使用它。
where : [数组,可选]真值表示要计算通用
该位置的function(ufunc),False值表示离开
仅在输出中的值。 
返回:
An array with arr1/arr2(element-wise) as elements of output array.
代码1:arr1除以arr2元素
# Python program explaining 
# divide() function 
import numpy as np 
  
# input_array 
arr1 = [2, 27, 2, 21, 23] 
arr2 = [2, 3, 4, 5, 6] 
print ("arr1         : ", arr1) 
print ("arr2         : ", arr2) 
  
# output_array 
out = np.divide(arr1, arr2) 
print ("\nOutput array : \n", out)输出:
arr1 : [2, 27, 2, 21, 23] arr2 : [2, 3, 4, 5, 6] Output array : [ 1. 9. 0.5 4.2 3.83333333]
代码2:arr1的元素除以除数
# Python program explaining 
# divide() function 
import numpy as np 
  
# input_array 
arr1 = [2, 27, 2, 21, 23] 
divisor = 3
print ("arr1         : ", arr1) 
  
# output_array 
out = np.divide(arr1, divisor) 
print ("\nOutput array : \n", out)输出:
arr1 : [2, 27, 2, 21, 23] Output array : [ 0.66666667 9. 0.66666667 7. 7.66666667]
代码3:警告如果arr2的元素= 0
# Python program explaining 
# divide() function 
import numpy as np 
  
# input_array 
arr1 = [2, 27, 2, 21, 23] 
arr2 = [2, 3, 0, 5, 6] 
print ("arr1         : ", arr1) 
print ("arr2         : ", arr2) 
  
# output_array 
out = np.divide(arr1, arr2) 
print ("\nOutput array : ", out)输出:
arr1 : [2, 27, 2, 21, 23] arr2 : [2, 3, 0, 5, 6] Output array : [ 1. 9. inf 4.2 3.83333333] RuntimeWarning: divide by zero encountered in true_divide out = np.power(arr1, arr2)
参考文献:
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.divide.html
。
相关用法
注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 numpy.divide() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
