多个逻辑参数的Numpy`logical_or` [英] Numpy `logical_or` for more than two arguments
问题描述
Numpy的logical_or
函数需要不超过两个数组进行比较.如何找到两个以上数组的并集? (关于Numpy的logical_and
并获得两个以上数组的交集,可能会问相同的问题.)
Numpy's logical_or
function takes no more than two arrays to compare. How can I find the union of more than two arrays? (The same question could be asked with regard to Numpy's logical_and
and obtaining the intersection of more than two arrays.)
推荐答案
If you're asking about numpy.logical_or
, then no, as the docs explicitly say, the only parameters are x1, x2
, and optionally out
:
numpy.
logical_or
(x1, x2[, out]
)=<ufunc 'logical_or'>
numpy.
logical_or
(x1, x2[, out]
) =<ufunc 'logical_or'>
您当然可以像这样将多个logical_or
调用链接在一起:
You can of course chain together multiple logical_or
calls like this:
>>> x = np.array([True, True, False, False])
>>> y = np.array([True, False, True, False])
>>> z = np.array([False, False, False, False])
>>> np.logical_or(np.logical_or(x, y), z)
array([ True, True, True, False], dtype=bool)
The way to generalize this kind of chaining in NumPy is with reduce
:
>>> np.logical_or.reduce((x, y, z))
array([ True, True, True, False], dtype=bool)
当然,如果您使用一个多维数组而不是单独的数组,这也将起作用—实际上,这就是使用 meant 的方式:
And of course this will also work if you have one multi-dimensional array instead of separate arrays—in fact, that's how it's meant to be used:
>>> xyz = np.array((x, y, z))
>>> xyz
array([[ True, True, False, False],
[ True, False, True, False],
[False, False, False, False]], dtype=bool)
>>> np.logical_or.reduce(xyz)
array([ True, True, True, False], dtype=bool)
但是,用NumPy术语来说,三个等长一维数组的元组是 array_like ,并且可以用作2D数组.
But a tuple of three equal-length 1D arrays is an array_like in NumPy terms, and can be used as a 2D array.
在NumPy之外,您还可以使用Python的reduce
:
Outside of NumPy, you can also use Python's reduce
:
>>> functools.reduce(np.logical_or, (x, y, z))
array([ True, True, True, False], dtype=bool)
但是,与NumPy的reduce
不同,Python并不是经常需要的.在大多数情况下,有一种更简单的处理方式-例如,将多个Python or
运算符链接在一起,不要将reduce
放在operator.or_
之上,而只需使用any
.而且当没有 时,使用显式循环通常更易读.
However, unlike NumPy's reduce
, Python's is not often needed. For most cases, there's a simpler way to do things—e.g., to chain together multiple Python or
operators, don't reduce
over operator.or_
, just use any
. And when there isn't, it's usually more readable to use an explicit loop.
实际上NumPy的 any
可以也可以用于这种情况,尽管它并不那么琐碎;如果您未明确为其指定轴,则最终将得到标量而不是数组.所以:
And in fact NumPy's any
can be used for this case as well, although it's not quite as trivial; if you don't explicitly give it an axis, you'll end up with a scalar instead of an array. So:
>>> np.any((x, y, z), axis=0)
array([ True, True, True, False], dtype=bool)
如您所料, logical_and
相似-您可以将其链接,np.reduce
,functools.reduce
或替换为
As you might expect, logical_and
is similar—you can chain it, np.reduce
it, functools.reduce
it, or substitute all
with an explicit axis
.
关于其他操作,例如 all
/any
类型的函数外,其他都是相同的. (你叫什么?odd
?)
What about other operations, like logical_xor
? Again, same deal… except that in this case there is no all
/any
-type function that applies. (What would you call it? odd
?)
这篇关于多个逻辑参数的Numpy`logical_or`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!