该部分位于numpy - ref - 1.14.5中的2.8 available ufuncs
1 数学运算
加法规则:
numpy.add(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘add‘>
x1 ,x2 - number,ndarray,Iterable 均可,实际上一般为ndarray。
out - 存储结果的位置,如果提供,其必须事具有输入广播的形状,如果无或者不提供,则返回新分配的数组;元组(可能仅作为关键字参数)的长度必须等于输出的数量。
余下参数略,不做说明,实际上记住 numpy.add(x1, x2) 即可,余下参数用的不多
示例一:一个ndarray + 数字
将数字与ndarray中的每一个元素相加
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.add(a,2) print(b) # [[3 4] # [5 6]]
示例二:两个同shape的ndarray相加
对应元素相加
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.arange(0,4).reshape(2,2) print(b) # [[0 1] # [2 3]] c = np.add(a,b) print(c) # [[1 3] # [5 7]]
示例三:两个shape不全相等的ndarray相加
此时相当于 广播
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.arange(0,2) print(b) # [0 1] c = np.add(a,b) print(c) # [[1 3] # [3 5]]
示例四:两个shape不全相等的ndarray相加
此时 与 广播 等价
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.arange(0,2).reshape(-1,1) print(b) # [[0] # [1]] c = np.add(a,b) print(c) # [[1 2] # [4 5]]
示例五:两个shape不全相等的ndarray相加
此时相当于广播
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.arange(0,4).reshape(1,2,2) print(b) # [[[0 1] # [2 3]]] c = np.add(a,b) print(c) # [[[1 3] # [5 7]]]
示例六:x1 x2的shape值不符合广播规则时,无法add
a = np.arange(1,5).reshape(2,2) print(a) # [[1 2] # [3 4]] b = np.arange(0,4) print(b) # [0 1 2 3] c = np.add(a,b) print(c) # ValueError: operands could not be broadcast together with shapes (2,2) (4,)
a = np.arange(1,7).reshape(2,3) print(a) # [[1 2 3] # [4 5 6]] b = np.arange(0,4).reshape(2,2) print(b) # [[0 1] # [2 3]] c = np.add(a,b) print(c) # ValueError: operands could not be broadcast together with shapes (2,3) (2,2)
综上:当x1.shape != x2.shape 时,x1 x2 必须可广播到一个通用形状(可能是其中一个或另一个形状shape)they must be broadcastable to a common shape (which may be the shape of one or the other)。
add(x1, x2, /[, out, where, casting, order, . . . ]) 按元素添加参数 Add arguments element-wise. subtract(x1, x2, /[, out, where, casting, . . . ]) Subtract arguments, element-wise. multiply(x1, x2, /[, out, where, casting, . . . ]) Multiply arguments element-wise. divide(x1, x2, /[, out, where, casting, . . . ]) Returns a true division of the inputs, element-wise. logaddexp(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs. logaddexp2(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs in base-2. true_divide(x1, x2, /[, out, where, . . . ]) Returns a true division of the inputs, element-wise. floor_divide(x1, x2, /[, out, where, . . . ]) Return the largest integer smaller or equal to the division of the inputs. negative(x, /[, out, where, casting, order, . . . ]) Numerical negative, element-wise. positive(x, /[, out, where, casting, order, . . . ]) Numerical positive, element-wise. power(x1, x2, /[, out, where, casting, . . . ]) First array elements raised to powers from second array, element-wise. remainder(x1, x2, /[, out, where, casting, . . . ]) Return element-wise remainder of division. mod(x1, x2, /[, out, where, casting, order, . . . ]) Return element-wise remainder of division. fmod(x1, x2, /[, out, where, casting, . . . ]) Return the element-wise remainder of division. divmod(x1, x2[, out1, out2], / [[, out, . . . ]) Return element-wise quotient and remainder simultane ously. absolute(x, /[, out, where, casting, order, . . . ]) Calculate the absolute value element-wise. fabs(x, /[, out, where, casting, order, . . . ]) Compute the absolute values element-wise. rint(x, /[, out, where, casting, order, . . . ]) Round elements of the array to the nearest integer. sign(x, /[, out, where, casting, order, . . . ]) Returns an element-wise indication of the sign of a number. heaviside(x1, x2, /[, out, where, casting, . . . ]) Compute the Heaviside step function. conj(x, /[, out, where, casting, order, . . . ]) Return the complex conjugate, element-wise. exp(x, /[, out, where, casting, order, . . . ]) Calculate the exponential of all elements in the input array. exp2(x, /[, out, where, casting, order, . . . ]) Calculate 2**p for all p in the input array. log(x, /[, out, where, casting, order, . . . ]) Natural logarithm, element-wise.
add(x1, x2, /[, out, where, casting, order, . . . ]) Add arguments element-wise.
subtract(x1, x2, /[, out, where, casting, . . . ]) Subtract arguments, element-wise.
multiply(x1, x2, /[, out, where, casting, . . . ]) Multiply arguments element-wise.
divide(x1, x2, /[, out, where, casting, . . . ]) Returns a true division of the inputs, element-wise.
logaddexp(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs.
logaddexp2(x1, x2, /[, out, where, casting, . . . ]) Logarithm of the sum of exponentiations of the inputs in
base-2.
true_divide(x1, x2, /[, out, where, . . . ]) Returns a true division of the inputs, element-wise.
floor_divide(x1, x2, /[, out, where, . . . ]) Return the largest integer smaller or equal to the division
of the inputs.
negative(x, /[, out, where, casting, order, . . . ]) Numerical negative, element-wise.
positive(x, /[, out, where, casting, order, . . . ]) Numerical positive, element-wise.
power(x1, x2, /[, out, where, casting, . . . ]) First array elements raised to powers from second array,
element-wise.
remainder(x1, x2, /[, out, where, casting, . . . ]) Return element-wise remainder of division.
mod(x1, x2, /[, out, where, casting, order, . . . ]) Return element-wise remainder of division.
fmod(x1, x2, /[, out, where, casting, . . . ]) Return the element-wise remainder of division.
divmod(x1, x2[, out1, out2], / [[, out, . . . ]) Return element-wise quotient and remainder simultane
ously.
absolute(x, /[, out, where, casting, order, . . . ]) Calculate the absolute value element-wise.
fabs(x, /[, out, where, casting, order, . . . ]) Compute the absolute values element-wise.
rint(x, /[, out, where, casting, order, . . . ]) Round elements of the array to the nearest integer.
sign(x, /[, out, where, casting, order, . . . ]) Returns an element-wise indication of the sign of a number.
heaviside(x1, x2, /[, out, where, casting, . . . ]) Compute the Heaviside step function.
conj(x, /[, out, where, casting, order, . . . ]) Return the complex conjugate, element-wise.
exp(x, /[, out, where, casting, order, . . . ]) Calculate the exponential of all elements in the input array.
exp2(x, /[, out, where, casting, order, . . . ]) Calculate 2**p for all p in the input array.
log(x, /[, out, where, casting, order, . . . ]) Natural logarithm, element-wise.
原文地址:https://www.cnblogs.com/gengyi/p/9231834.html