Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回数组的函数的numpy.vectorize_Python_Numpy_Vectorization - Fatal编程技术网

Python 返回数组的函数的numpy.vectorize

Python 返回数组的函数的numpy.vectorize,python,numpy,vectorization,Python,Numpy,Vectorization,我试图对一个函数进行矢量化,该函数有2个输入,并输出一个np.array,shape=(4,)。函数如下所示: def f(a, b): return np.array([a+b, a-b, a, b]) 我能够使用signature参数对函数进行矢量化,但是,只有在使用np的excluded参数排除其中一个参数时,它才会起作用。矢量化: 这是有效的: vec = np.vectorize(f, signature='()->(n)', excluded=[1]) x = np.

我试图对一个函数进行矢量化,该函数有2个输入,并输出一个np.array,shape=(4,)。函数如下所示:

def f(a, b):
    return np.array([a+b, a-b, a, b])
我能够使用signature参数对函数进行矢量化,但是,只有在使用
np的
excluded
参数排除其中一个参数时,它才会起作用。矢量化

这是有效的:

vec = np.vectorize(f, signature='()->(n)', excluded=[1])
x = np.arange(5)
y = 3
vec(x, y)

>> output:
array([[ 3, -3,  0,  3],
       [ 4, -2,  1,  3],
       [ 5, -1,  2,  3],
       [ 6,  0,  3,  3],
       [ 7,  1,  4,  3]])
vec = np.vectorize(f, signature='()->(n)')
x = np.arange(5)
y = 3
vec(x, y)

>> Error:
TypeError: wrong number of positional arguments: expected 1, got 2
但是,如果我去掉
excluded
参数,事情就不会按计划进行了

这不起作用:

vec = np.vectorize(f, signature='()->(n)', excluded=[1])
x = np.arange(5)
y = 3
vec(x, y)

>> output:
array([[ 3, -3,  0,  3],
       [ 4, -2,  1,  3],
       [ 5, -1,  2,  3],
       [ 6,  0,  3,  3],
       [ 7,  1,  4,  3]])
vec = np.vectorize(f, signature='()->(n)')
x = np.arange(5)
y = 3
vec(x, y)

>> Error:
TypeError: wrong number of positional arguments: expected 1, got 2
如何使矢量化函数能够接收一个(或两个)输入参数的输入值数组/列表

预期的输出将是一个
vec
函数,该函数将允许为任一输入参数调用多个输入

In [237]: f1 = np.vectorize(f, signature='(),()->(n)')                          
In [238]: f1(np.arange(5),3)                                                    
Out[238]: 
array([[ 3, -3,  0,  3],
       [ 4, -2,  1,  3],
       [ 5, -1,  2,  3],
       [ 6,  0,  3,  3],
       [ 7,  1,  4,  3]])

In [241]: f1(np.arange(5),np.ones((4,5))).shape                                 
Out[241]: (4, 5, 4)
In [242]: f1(np.arange(5),np.ones((1,5))).shape                                 
Out[242]: (1, 5, 4)

frompyfunc
返回对象数据类型数组:

In [336]: f2 = np.frompyfunc(f,2,1)                                             
In [337]: f2(np.arange(5), 3)                                                   
Out[337]: 
array([array([ 3, -3,  0,  3]), array([ 4, -2,  1,  3]),
       array([ 5, -1,  2,  3]), array([6, 0, 3, 3]), array([7, 1, 4, 3])],
      dtype=object)
In [338]: _.shape                                                               
Out[338]: (5,)
np.vectorize
,不带
签名
,使用
frompyfunc
,但添加自己的
dtype
转换


此操作失败的原因与以下添加失败的原因相同:

In [341]: np.arange(5)+np.arange(3)                                             
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-341-fb1c4f4372da> in <module>
----> 1 np.arange(5)+np.arange(3)

ValueError: operands could not be broadcast together with shapes (5,) (3,) 

按照你的标准,相当于单音节;-)@保尔·帕尼泽,我对这种签名方法的感觉不一。这是一个有趣的想法,但它的运行速度甚至比常规的
vectorize
@hpaulj还要慢,我期待着另一种行为。。。也许矢量化不是我应该使用的功能?对于
f1(x,y)
,其中x有n个元素,y有m个元素,我希望返回一个维数为nxm的数组,其中每个元素
f(x\u I,y\u j)
For
0@hpaulj不确定为什么我们需要传递一个2-dim数组作为第二个参数。我希望调用
f1(np.arange(5),np.arange(3))
得到一个形状(3,5,4)(4,5,4)数组是一个(n,m,4)数组。我刚刚添加了一个来自pyfunc的
示例,该示例创建了一个(n,m)对象数据类型数组,其中每个元素都是4元素数组。