numpy.logical_and和&之间的区别 [英] Difference between numpy.logical_and and &

查看:850
本文介绍了numpy.logical_and和&之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个或多个numpy数组的logical_and.我知道numpy具有功能logical_and(),但是我发现简单的运算符&返回相同的结果,并且可能更易于使用.

I'm trying to use the logical_and of two or more numpy arrays. I know numpy has the function logical_and(), but I find the simple operator & returns the same results and are potentially easier to use.

例如,考虑三个numpy数组a,b和c.是 np.logical_and(a, np.logical_and(b,c)) 相当于 a & b & c?

For example, consider three numpy arrays a, b, and c. Is np.logical_and(a, np.logical_and(b,c)) equivalent to a & b & c?

如果它们(或多或少)等效,那么使用logical_and()有什么好处?

If they are (more or less) equivalent, what's the advantage of using logical_and()?

推荐答案

@ user1121588在评论中回答了大部分问题,但要完全回答...

@user1121588 answered most of this in a comment, but to answer fully...

按位与"(&)在布尔数组上的行为与logical_and几乎相同,但是它不能传达意图,也不能像使用logical_and那样使用,并且增加了在非布尔数组中获得误导性答案的可能性. -无关紧要的情况(可能是压缩数组或稀疏数组).

"Bitwise and" (&) behaves much the same as logical_and on boolean arrays, but it doesn't convey the intent as well as using logical_and, and raises the possibility of getting misleading answers in non-trivial cases (packed or sparse arrays, maybe).

要在多个数组上使用逻辑逻辑,请执行以下操作:

To use logical_and on multiple arrays, do:

np.logical_and.reduce([a, b, c])

,其中参数是您希望一起logical_and的数组的列表.它们都应该是相同的形状.

where the argument is a list of as many arrays as you wish to logical_and together. They should all be the same shape.

这篇关于numpy.logical_and和&之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆