python - 返回数组的函数的 numpy.vectorize

标签 python numpy vectorization

我正在尝试向量化一个具有 2 个输入的函数,并输出一个形状为 =(4,) 的 np.array。该函数如下所示:

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

我能够使用签名参数对函数进行矢量化,但是只有当我使用 np.vectorizeexcluded 参数排除其中一个参数时,它才有效:

这有效:

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

但是,如果我去掉 excluded 参数,事情就不会按计划进行。

这不起作用:

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 [340]: f1(np.arange(5), np.arange(3)) 
ValueError: shape mismatch: objects cannot be broadcast to a single shape

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

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

要获得 (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]])

关于python - 返回数组的函数的 numpy.vectorize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55330169/

相关文章:

python - 类型错误 : Object of type bool_ is not JSON serializable

python - 将 CSV 文件读取到 numpy 数组,第一行为字符串,其余为 float

Python 本地最小值/最大值,而 bin 没有改变

python - 如何在 Komodo Edit/IDE 中设置新文件的文件类型?

python - 数据框 View 或副本有什么好处

python - 如何使自定义 C PyTypeObject 与 python 的 "in"关键字一起使用?

r - 从一组对中,找到所有子集 s.t.子集中没有对与不在子集中的对共享任何元素

python - 我怎么知道 Pandas 数据框单元格的类型

r - charToRaw 的矢量化版本,具有良好的性能

arrays - 从索引数组创建二进制矩阵