一 、 逻辑运算
1. numpy.logical_and
import numpy as np
np.logical_and(True, False)
False
np.logical_and([True, False], [False, False])
array([False, False])
x = np.arange(5)
np.logical_and(x>1, x<4)
array([False, False, True, True, False])
- 简写方式-符号
a = np.array([True, False])
b = np.array([False, False])
a & b
array([False, False])
2. numpy.logical_or
np.logical_or(True, False)
True
np.logical_or([True, False], [False, False])
array([ True, False])
x = np.arange(5)np.logical_or(x < 1, x > 3)
array([ True, False, False, False, True])
- 简写方式-符号
a = np.array([True, False])b = np.array([False, False])a | b
array([ True, False])
3. numpy.logical_not
np.logical_not(3)
False
np.logical_not([True, False, 0, 1])
array([False, True, True, False])
x = np.arange(5)np.logical_not(x<3)
array([False, False, False, True, True])
4. numpy.logical_xor 异或
np.logical_xor(True, False)
True
np.logical_xor([True, True, False, False], [True, False, True, False])
array([False, True, True, False])
x = np.arange(5)np.logical_xor(x < 1, x > 3)
array([ True, False, False, False, True])
np.logical_xor(0, np.eye(2))
array([[ True, False], [False, True]])
二、比较
1. numpy.array_equal
True if two arrays have the same shape and elements, False otherwise.
np.array_equal([1, 2], [1, 2])
True
np.array_equal(np.array([1, 2]), np.array([1, 2]))
True
np.array_equal([1, 2], [1, 2, 3])
False
np.array_equal([1, 2], [1, 4])
False
a = np.array([1, np.nan])np.array_equal(a, a)
False
np.array_equal(a, a, equal_nan=True)
True
a = np.array([1 + 1j]) # 复数b = a.copy()a.real = np.nanb.imag = np.nannp.array_equal(a, b, equal_nan=True)
True
2. numpy.array_equiv
Returns True if input arrays are shape consistent and all elements equal.
np.array_equiv([1, 2], [1, 2])
True
np.array_equiv([1, 2], [1, 3])
False
np.array_equiv([1, 2], [[1, 2], [1, 2]])
True
np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]])
False
np.array_equiv([1, 2], [[1, 2], [1, 3]])
False
3. numpy.greater
Return the truth value of (x1 > x2) element-wise
np.greater([4,2],[2,2])
array([ True, False])
a = np.array([4, 2])b = np.array([2, 2])a > b
array([ True, False])
4. numpy.greater_equal
Return the truth value of (x1 >= x2) element-wise
np.greater_equal([4, 2, 1], [2, 2, 2])
array([ True, True, False])
a = np.array([4, 2, 1])b = np.array([2, 2, 2])a >= b
array([ True, True, False])
5. numpy.less
Return the truth value of (x1 < x2) element-wise.
np.less([1, 2], [2, 2])
array([ True, False])
a = np.array([1, 2])b = np.array([2, 2])a < b
array([ True, False])
6. numpy.less_equal
Return the truth value of (x1 <= x2) element-wise.
np.less_equal([4, 2, 1], [2, 2, 2])
array([False, True, True])
a = np.array([4, 2, 1])b = np.array([2, 2, 2])a <= b
array([False, True, True])
7. numpy.equal
Return (x1 == x2) element-wise x1, x2:array_like
np.equal([0, 1, 3], np.arange(3))
array([ True, True, False])
np.equal(1, np.ones(1))
array([ True])
a = np.array([2, 4, 6])b = np.array([2, 4, 2])a == b
array([ True, True, False])
8. numpy.not_equal
Return (x1 != x2) element-wise. x1, x2:array_like
np.not_equal([1.,2.], [1., 3.])
array([False, True])
np.not_equal([1, 2], [[1, 3],[1, 4]])
array([[False, True],
[False, True]])
a = np.array([1., 2.])
b = np.array([1., 3.])
a != b
array([False, True])