Python 将lambda放入NumPy`np.fromfunction()`会导致TypeError

Python 将lambda放入NumPy`np.fromfunction()`会导致TypeError,python,numpy,Python,Numpy,我正在努力理解NumPy np.fromfunction 下面这段代码就是从中提取出来的 输出 2.0 正如所料 当我把它们放在一起,在np.from函数中使用np.linalg.norm时 ds = np.array([[1, 2, 1], [1, 1, 0], [0, 1, 1]]) np.fromfunction(f, ds.shape) 出现错误 > TypeError Tracebac

我正在努力理解NumPy np.fromfunction

下面这段代码就是从中提取出来的

输出

2.0
正如所料

当我把它们放在一起,在np.from函数中使用np.linalg.norm时

ds = np.array([[1, 2, 1],
       [1, 1, 0],
       [0, 1, 1]])
np.fromfunction(f, ds.shape)
出现错误

> TypeError                                 Traceback (most recent call
last) <ipython-input-6-f5d65a6d95c4> in <module>()
      2        [1, 1, 0],
      3        [0, 1, 1]])
----> 4 np.fromfunction(f, ds.shape)

~/anaconda3/envs/tf11/lib/python3.6/site-packages/numpy/core/numeric.py
in fromfunction(function, shape, **kwargs)    
2026     dtype = kwargs.pop('dtype', float)    
2027     args = indices(shape, dtype=dtype)
-> 2028     return function(*args, **kwargs)    
2029     
2030 
TypeError: <lambda>() takes 1 positional argument but 2 were given

是否可以将lambda函数放入np.fromfunction中的另一个lambda函数。若要执行此操作,请获取距离数组?

当您使用二维数组时,您的函数需要接受两个输入

np.fromfunction的文档说,通过在每个坐标上执行函数来构造数组

因此它将传递数组中每个元素的坐标0,0,然后0,1。。。等来构造数组。这真的是你想要做的吗

您可以将lambda更改为类似的内容,但这实际上取决于您尝试执行的操作

f = lambda x, y: np.linalg.norm([x,y],1)

当您使用二维数组时,函数需要接受2个输入

np.fromfunction的文档说,通过在每个坐标上执行函数来构造数组

因此它将传递数组中每个元素的坐标0,0,然后0,1。。。等来构造数组。这真的是你想要做的吗

您可以将lambda更改为类似的内容,但这实际上取决于您尝试执行的操作

f = lambda x, y: np.linalg.norm([x,y],1)
请看错误:

In [171]: np.fromfunction(f, ds.shape)                                               
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-171-1a3ed1ade41a> in <module>
----> 1 np.fromfunction(f, ds.shape)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in fromfunction(function, shape, **kwargs)
   2026     dtype = kwargs.pop('dtype', float)
   2027     args = indices(shape, dtype=dtype)
-> 2028     return function(*args, **kwargs)
   2029 
   2030 

TypeError: <lambda>() takes 1 positional argument but 2 were given
这是一个2,3,3数组。2来自2元素形状,3,3来自形状本身。这类似于np.meshgrid和np.mgrid生成的内容。只是索引数组

然后它将该数组传递给函数,并将*args解包

function(*args, **kwargs)

In [174]: f(Out[172][0], Out[172][1])                                                
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-174-40469f1ab449> in <module>
----> 1 f(Out[172][0], Out[172][1])

TypeError: <lambda>() takes 1 positional argument but 2 were given
===

在链接的SO中,lambda接受2个参数,lambda x,y:

因此,无论是问题还是答案,ds数组只是形状的来源,目标是0,1,ds的最大元素

实际上,链接答案中的from函数只是执行以下操作:

In [180]: f1 = lambda x,y: np.abs(0-x) + np.abs(1-y)                                 
In [181]: f1(Out[172][0], Out[172][1])                                               
Out[181]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [182]: np.abs(0-Out[172][0]) + np.abs(1-Out[172][1])                              
Out[182]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [183]: np.abs(np.array([0,1])[:,None,None]-Out[172]).sum(axis=0)                  
Out[183]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [184]: np.abs(0-np.arange(3))[:,None] + np.abs(1-np.arange(3))                    
Out[184]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])
请看错误:

In [171]: np.fromfunction(f, ds.shape)                                               
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-171-1a3ed1ade41a> in <module>
----> 1 np.fromfunction(f, ds.shape)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in fromfunction(function, shape, **kwargs)
   2026     dtype = kwargs.pop('dtype', float)
   2027     args = indices(shape, dtype=dtype)
-> 2028     return function(*args, **kwargs)
   2029 
   2030 

TypeError: <lambda>() takes 1 positional argument but 2 were given
这是一个2,3,3数组。2来自2元素形状,3,3来自形状本身。这类似于np.meshgrid和np.mgrid生成的内容。只是索引数组

然后它将该数组传递给函数,并将*args解包

function(*args, **kwargs)

In [174]: f(Out[172][0], Out[172][1])                                                
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-174-40469f1ab449> in <module>
----> 1 f(Out[172][0], Out[172][1])

TypeError: <lambda>() takes 1 positional argument but 2 were given
===

在链接的SO中,lambda接受2个参数,lambda x,y:

因此,无论是问题还是答案,ds数组只是形状的来源,目标是0,1,ds的最大元素

实际上,链接答案中的from函数只是执行以下操作:

In [180]: f1 = lambda x,y: np.abs(0-x) + np.abs(1-y)                                 
In [181]: f1(Out[172][0], Out[172][1])                                               
Out[181]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [182]: np.abs(0-Out[172][0]) + np.abs(1-Out[172][1])                              
Out[182]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [183]: np.abs(np.array([0,1])[:,None,None]-Out[172]).sum(axis=0)                  
Out[183]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

In [184]: np.abs(0-np.arange(3))[:,None] + np.abs(1-np.arange(3))                    
Out[184]: 
array([[1, 0, 1],
       [2, 1, 2],
       [3, 2, 3]])

你为什么认为这会起作用?你期望它做什么?记住,当你发布的代码不能做你想要的,我们可以看到它做什么,但不能看到它应该做什么。你必须单独告诉我们。为什么你认为这会起作用?你期望它做什么?记住,当你发布的代码不能做你想要的,我们可以看到它做什么,但不能看到它应该做什么。你必须告诉我们分离函数。没有函数函数的编译魔法意味着在Python中实现,而不是C++ +?@ CZLSWS,它很容易查看DOCS页面上的函数,如FasFuff.E.G[源]链接。我们的错误消息还显示了相关的Python代码。它所做的只是调用我们提供的函数。它不尝试对我们的函数进行任何“编译”。如果你想编译你的函数的版本,使用NUBBA或Cython。没有编译的魔法函数函数在Python中实现,而不是C++ +?@ CZLSWS,它很容易查看DOCS页面上的函数,比如FaskFuff.E.G[源]链接。我们的错误消息还显示了相关的Python代码。它所做的只是调用我们提供的函数。它不尝试对我们的函数进行任何“编译”。如果您想要函数的编译版本,请使用numba或cython。