小编Pat*_*o P的帖子

返回数组的函数的 numpy.vectorize

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

def f(a, b):
    return np.array([a+b, a-b, a, b])
Run Code Online (Sandbox Code Playgroud)

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

这有效:

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]])
Run Code Online (Sandbox Code Playgroud)

然而,如果我取消excluded争论,事情就不会按计划进行。

这不起作用:

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

>> Error:
TypeError: wrong …
Run Code Online (Sandbox Code Playgroud)

python numpy vectorization

5
推荐指数
1
解决办法
3167
查看次数

标签 统计

numpy ×1

python ×1

vectorization ×1