Numpy中的np.logical_or()- Python

jm2pwxwz  于 2023-03-30  发布在  Python
关注(0)|答案(1)|浏览(141)

请任何人都可以解释我这个结果代码

x = np.array([9, 5])
y = np.array([16, 12])

np.logical_or(x < 5, y > 15)

结果-〉array([ True, False])
自:

  1. np.logical_or(x < 5, y > 15)
  2. x < 5 = [False, False], y > 15 = [True, False]
    我想应该是:
[False or False, False or True] = [False, True]

实际上Python给出的结果是这样的:

[False or True, False or False] = [True, False]

为什么结果不是[False,True],而实际上python给出的结果是[True,False]?这与操作np.logical_or(x < 5, y > 15)的顺序不匹配
即使我尝试了ChatGPT的解释,我仍然没有清楚地理解这个概念。
如果有人能一步一步地详细解释python进程的背景,我将不胜感激。

ippsafx7

ippsafx71#

你对结果的解释是错误的。Numpy的logical_or可以有几个参数作为过滤器。在你的例子中,输入参数是x < 5y > 15。然后它用or运算符将它们组合在一起。

  • 第一个参数x < 5将导致:[False, False]
  • 第二个参数y > 15将导致:[True, False]
  • 最终结果= [False or True, False or False] = [True, False]

相关问题