简体   繁体   English

返回数组的函数的numpy.vectorize

[英]numpy.vectorize of function that returns an array

I am trying to vectorize a function which has 2 inputs, and outputs a np.array, of shape =(4,). 我试图向量化一个具有2个输入并输出形状=(4,)的np.array的函数。 The function looks like this: 该函数如下所示:

def f(a, b):
    return np.array([a+b, a-b, a, b])

I was able to vectorize the function, using the signature parameter, however it only works if I exclude one of the parameters using the excluded argument of np.vectorize : 我能够使用签名参数对函数进行矢量化处理,但是仅当我使用np.vectorizeexcluded参数排除其中一个参数时,该方法才有效:

This works: 这有效:

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]])

However if I take out the excluded argument, things don't go as planned. 但是,如果我排除了excluded参数,事情就不会按计划进行。

This does not work: 这不起作用:

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

How can I make the vectorized function able to receive an array/list of input values for either one (or both) of the input parameters? 如何使向量化函数能够接收一个(或两个)输入参数的输入值的数组/列表?

The expected output would be a vec function which would enable calling it with multiple inputs for either one of the input parameters. 预期的输出将是一个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 returns a object dtype array: frompyfunc返回一个对象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 , without the signature , uses frompyfunc , but adds its own dtype conversion. np.vectorize ,没有signature ,使用frompyfunc ,但增加了自己的dtype转换。


In [340]: f1(np.arange(5), np.arange(3)) 
ValueError: shape mismatch: objects cannot be broadcast to a single shape

This fails for the same reason the following addition fails: 失败的原因相同,以下添加失败:

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,) 

To get a (5,3) result we need to make the first argument (5,1) shape: 要获得(5,3)结果,我们需要使第一个参数(5,1)成形:

In [342]: np.arange(5)[:,None]+np.arange(3)                                     
Out[342]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5],
       [4, 5, 6]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM