zoukankan      html  css  js  c++  java
  • Numpy 数据的元素级逻辑运算 np.logical_and、np.logical_or、np.logical_not

      在大神麦金尼的著作中,对 np.logical_and、np.logical_or、np.logical_xor 三个二元通用函数的说明是:Computer element_wise true value of logical operateion (equivalent to infix operators & ,  |, ^

    代码体验示例:

    In [1]: import numpy as np

    In [2]: a = [0, 1, 2, 3, 0]

    In [3]: b = [1, 1, 2, 3, 1]

    In [4]: np.logical_and(a, b)  # 对每个元素进行 and 运算,返回 bool 值组成的数组
    Out[4]: array([False,  True,  True,  True, False], dtype=bool)

    In [5]: np.logical_or(a, b)  # 对每个元素进行 or 运算,返回 bool 值组成的数组
    Out[5]: array([ True,  True,  True,  True,  True], dtype=bool)

    In [6]: np.logical_xor(a, b) # 对每个元素进行 异或 运算,返回 bool 值组成的数组
    Out[6]: array([ True, False, False, False,  True], dtype=bool)

     

    说明:

    np.logical_and、np.logical_or、np.logical_xor 在执行元素级运算时,语法为 np.logical_and(x1, x2), np.logical_or(x1, x2), np.logical_xor(x1, x2)

    x1、 x2 是列表、数组一类的可以转换成 array 结构的数据容器。

    x1、x2 的形状大小需相同。

    x1、x2 的数据类型需是 int、float, bool。

    返回的结果是有 True 或 False 组成的形状和 x1、x2 相同的 Numpy 数组。

     

  • 相关阅读:
    Zset-ZREVRANGEBYSCORE
    Zset-ZREVERANGE
    Zset-ZRANGEBYSCORE
    Leetcode1550. 存在连续三个奇数的数组
    Java中的IO流
    线程间通信(也叫线程并发协作)的四种方式
    数据库三大范式
    MVCC(Multi-Version Concurrency Control):多版本并发控制详解
    Java三种单例模式实现
    Java的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/shanger/p/12925583.html
Copyright © 2011-2022 走看看