我正在尝试理解 NumPy np.fromfunction()
。
以下代码是从这个 post 中提取的.
dist = np.array([ 1, -1])
f = lambda x: np.linalg.norm(x, 1)
f(dist)
输出
2.0
符合预期。
当我将它们放在一起以在 np.fromfunction() 中使用 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
是否可以在 np.fromfunction() 中放置一个 lambda 函数(可能是另一个 lambda 函数)来完成这项工作(获取距离数组)?
最佳答案
查看错误:
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
fromfunction
是一个小的 Python 函数;没有编译魔法。
根据您给出的形状,它生成索引
。
In [172]: np.indices(ds.shape)
Out[172]:
array([[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]])
这是一个 (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
这就是它所做的全部 - 生成一个 n 维网格,并将其作为 n 个参数传递给您的函数。
===
另请注意,您将 ds.shape
传递给了 fromfunction
,而不是 ds
本身。您也可以编写 np.fromfunction(f,(3,3))
。
您希望您的 lambda
对 ds
做什么?显然 fromfunction
没有为您做这件事。
===
有了这个f
,fromfunction
唯一能做的就是给它一个arange
:
In [176]: np.fromfunction(f, (10,))
Out[176]: 45.0
In [177]: f(np.arange(10))
Out[177]: 45.0
===
在链接的 SO 中,lambda 接受 2 个参数,lambda x,y
:
np.fromfunction(lambda x,y: np.abs(target[0]-x) + np.abs(target[1]-y), ds.shape)
在那个SO中,无论是问题还是答案,ds
数组只是shape的来源,Target是(0,1),ds
的最大元素>。
实际上,链接答案中的 fromfunction
只是在做:
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 - 将 lambda 放入 NumPy `np.fromfunction()` 会导致 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55930785/