当函数包含条件时,使用Numpy将函数应用于数组 [英] Applying a function to an array using Numpy when the function contains a condition
问题描述
当函数包含条件时,我很难将函数应用于数组。我的解决方法效率低下,正在寻找一种高效(快速)的方法。在一个简单的示例中:
I am having a difficulty with applying a function to an array when the function contains a condition. I have an inefficient workaround and am looking for an efficient (fast) approach. In a simple example:
pts = np.linspace(0,1,11)
def fun(x, y):
if x > y:
return 0
else:
return 1
现在,如果我运行:
result = fun(pts, pts)
然后我得到错误
ValueError:具有多个元素的数组是不明确的。使用在
如果x>引发的a.any()或a.all()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
y 行。我的效率低下的解决方法给出了正确的结果,但是却太慢了:
raised at the if x > y
line. My inefficient workaround, which gives the correct result but is too slow is:
result = np.full([len(pts)]*2, np.nan)
for i in range(len(pts)):
for j in range(len(pts)):
result[i,j] = fun(pts[i], pts[j])
在更好(更重要的是,更快)的方法?
What is the best way to obtain this in a nicer (and more importantly, faster) way?
当函数包含条件时,我很难将函数应用于数组。我的解决方法效率低下,正在寻找一种高效(快速)的方法。在一个简单的示例中:
I am having a difficulty with applying a function to an array when the function contains a condition. I have an inefficient workaround and am looking for an efficient (fast) approach. In a simple example:
pts = np.linspace(0,1,11)
def fun(x, y):
if x > y:
return 0
else:
return 1
现在,如果我运行:
result = fun(pts, pts)
然后我得到错误
ValueError:具有多个元素的数组是不明确的。使用在
如果x>引发的a.any()或a.all()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
y 行。我的效率低下的解决方法给出了正确的结果,但是却太慢了:
raised at the if x > y
line. My inefficient workaround, which gives the correct result but is too slow is:
result = np.full([len(pts)]*2, np.nan)
for i in range(len(pts)):
for j in range(len(pts)):
result[i,j] = fun(pts[i], pts[j])
在更好(更重要的是,更快)的方式?
What is the best way to obtain this in a nicer (and more importantly, faster) way?
编辑:使用
def fun(x, y):
if x > y:
return 0
else:
return 1
x = np.array(range(10))
y = np.array(range(10))
xv,yv = np.meshgrid(x,y)
result = fun(xv, yv)
仍然引发相同的 ValueError
。
推荐答案
错误非常明显-假设您有
The error is quite explicit - suppose you have
x = np.array([1,2])
y = np.array([2,1])
使得
(x>y) == np.array([0,1])
如果 np.array([0,1])
语句?是真的还是假的? numpy
告诉您这是模棱两可的。使用
what should be the result of your if np.array([0,1])
statement? is it true or false? numpy
is telling you this is ambiguous. Using
(x>y).all()
或
(x>y).any()
是显式的,因此 numpy
为您提供解决方案-要么任何一个单元对都满足条件,要么全部都满足-两者都是明确的真值。您必须自己定义向量x大于向量y 的含义。
is explicit, and thus numpy
is offering you solutions - either any cell pair fulfills the condition, or all of them - both an unambiguous truth value. You have to define for yourself exactly what you meant by vector x is larger than vector y.
numpy
解决方案可在所有 x
和 y
对上运行,从而使 x [i]&y; y [j]
将使用网格生成所有对:
The numpy
solution to operate on all pairs of x
and y
such that x[i]>y[j]
is to use mesh grid to generate all pairs:
>>> import numpy as np
>>> x=np.array(range(10))
>>> y=np.array(range(10))
>>> xv,yv=np.meshgrid(x,y)
>>> xv[xv>yv]
array([1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8,
9, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 6, 7, 8, 9, 7, 8, 9, 8, 9, 9])
>>> yv[xv>yv]
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8])
要么发送 xv
和 yv
到 fun
,或在函数中创建网格,具体取决于哪个更有意义。这样会生成所有对 xi,yj
,这样 xi> yj
。如果您希望实际索引仅返回 xv> yv
,其中每个单元格 ij
对应 x [i]
和 y [j]
。在您的情况下:
either send xv
and yv
to fun
, or create the mesh in the function, depending on what makes more sense. This generates all pairs xi,yj
such that xi>yj
. If you want the actual indices just return xv>yv
, where each cell ij
corresponds x[i]
and y[j]
. In your case:
def fun(x, y):
xv,yv=np.meshgrid(x,y)
return xv>yv
将返回一个矩阵,其中 fun(x,y)[i] [j]
如果 x [i]> y [j]
为True,否则为False。或者,
will return a matrix where fun(x,y)[i][j]
is True if x[i]>y[j]
, or False otherwise. Alternatively
return np.where(xv>yv)
将返回两个索引对数组的元组,这样
will return a tuple of two arrays of pairs of the indices, such that
for i,j in fun(x,y):
将保证 x [i]> y [j]
。
这篇关于当函数包含条件时,使用Numpy将函数应用于数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!