简体   繁体   English

numpy.multiply最多可以有3个参数(操作数),有什么办法可以超过3个?

[英]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库。

You can use np.prod to calculate the product of array elements over a given axis , here it's (axis=0), ie multiply rows element-wise: 您可以使用np.prod来计算给定轴上的数组元素的乘积 ,这里是(axis = 0),即按元素乘以行:

np.prod([w, x, y], axis=0)
# array([ 6,  8, 36])

np.prod([w, x, y, z], axis=0)
# array([ 60,  80, 360])

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.

相关问题 为什么 numpy.multiply 对大整数给出错误的结果? - Why doe numpy.multiply give wrong result for large integers? 如何在NumPy中一次乘以3个以上的向量 - How can I multiply more than 3 vectors at once in NumPy numpy.multiply() 的后端源代码是否为多处理/多线程设置? - Is the backend source code for numpy.multiply() setup for multiprocessing/multithreading? 有没有办法在 ANFIS 中有超过 2 个输入? - Is there any way to have more than 2 inputs in ANFIS? 有没有其他方法可以使用多个 arguments 写入文件? - Is there any other way to write into a file using more than one arguments? numpy最多需要3个参数-一种解决方法? - numpy where takes at most 3 arguments - a way around this? 用于两个以上参数的 Numpy `logical_or` - Numpy `logical_or` for more than two arguments 在 numpy 中将小矩阵与标量相乘的最有效方法 - Most efficient way to multiply a small matrix with a scalar in numpy Python numpy.vectorize:ValueError:无法构造具有超过 32 个操作数的 ufunc - Python numpy.vectorize: ValueError: Cannot construct a ufunc with more than 32 operands 删除具有特定值大于给定数量(numpy)的行的最佳方法 - Best way to remove rows that have more of a certain value than a given amount (numpy)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM