[英]numpy.multiply can have at most 3 arguments (operands), is there any way to do more than 3?
By the following example, I can confirm that multiply would only work with at most 3 arguments: 通过以下示例,我可以确认乘法最多只能使用3个参数:
import numpy as np
w = np.asarray([2, 4, 6])
x = np.asarray([1, 2, 3])
y = np.asarray([3, 1, 2])
z = np.asarray([10, 10, 10])
np.multiply(w, x, y) # works
np.multiply(w, x, y, z) #failed
Here is the error message: 这是错误消息:
ValueError Traceback (most recent call last)
<ipython-input-14-9538812eb3b4> in <module>()
----> 1 np.multiply(w, x, y, z)
ValueError: invalid number of arguments
Is there any way to achieve multiply with more than 3 arguments? 有什么方法可以实现超过3个参数的乘法运算? I don't mind to use other Python library.
我不介意使用其他Python库。
Actually multiply
takes two arrays. 其实
multiply
需要两个数组。 It's a binary operation. 这是一个二进制操作。 The third is an optional
out
. 第三个是可选的
out
。 But as a ufunc
it has a reduce
method which takes a list: 但是作为
ufunc
它具有一个reduce
方法,该方法带有一个列表:
In [234]: x=np.arange(4)
In [235]: np.multiply.reduce([x,x,x,x])
Out[235]: array([ 0, 1, 16, 81])
In [236]: x*x*x*x
Out[236]: array([ 0, 1, 16, 81])
In [237]: np.prod([x,x,x,x],axis=0)
Out[237]: array([ 0, 1, 16, 81])
np.prod
can do the same, but be careful with the axis
parameter. np.prod
可以执行相同的操作,但请注意axis
参数。
More fun with ufunc
- accumulate: ufunc
更有趣-累积:
In [240]: np.multiply.accumulate([x,x,x,x])
Out[240]:
array([[ 0, 1, 2, 3],
[ 0, 1, 4, 9],
[ 0, 1, 8, 27],
[ 0, 1, 16, 81]], dtype=int32)
In [241]: np.cumprod([x,x,x,x],axis=0)
Out[241]:
array([[ 0, 1, 2, 3],
[ 0, 1, 4, 9],
[ 0, 1, 8, 27],
[ 0, 1, 16, 81]], dtype=int32)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.