https://github.com/datawhalechina/team-learning-program/tree/master/IntroductionToNumpy
逻辑函数
真值测试
numpy.all
numpy.any
- numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
- numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.
【例】
import numpy as np
a = np.array([0, 4, 5])
b = np.copy(a)
print(np.all(a == b)) # True
print(np.any(a == b)) # True
b[0] = 1
print(np.all(a == b)) # False
print(np.any(a == b)) # True
print(np.all([1.0, np.nan])) # True
print(np.any([1.0, np.nan])) # True
a = np.eye(3)
print(np.all(a, axis=0)) # [False False False]
print(np.any(a, axis=0)) # [ True True True]
数组内容
numpy.isnan
numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.
【例】
a=np.array([1,2,np.nan])
print(np.isnan(a))
#[False False True]
逻辑运算
numpy.logical_not
numpy.logical_and
numpy.logical_or
numpy.logical_xor
numpy.logical_not(x, *args, **kwargs)Compute the truth value of NOT x element-wise.
numpy.logical_and(x1, x2, *args, **kwargs) Compute the truth value of x1 AND x2 element-wise.
numpy.logical_or(x1, x2, *args, **kwargs)Compute the truth value of x1 OR x2 element-wise.
numpy.logical_xor(x1, x2, *args, **kwargs)Compute the truth value of x1 XOR x2, element-wise.
【例】计算非x元素的真值。
import numpy as np
print(np.logical_not(3))
# False
print(np.logical_not([True, False, 0, 1]))
# [False True True False]
x = np.arange(5)
print(np.logical_not(x < 3))
# [False False False True True]
【例】计算x1 AND x2元素的真值。
x = np.arange(5)
print(np.logical_and(True, False))
# False
print(np.logical_and([True, False], [True, False]))
# [ True False]
print(np.logical_and(x > 1, x < 4))
# [False False True True False]
【例】逐元素计算x1 OR x2的真值。
x = np.arange(5)
print(np.logical_or(True, False))
# True
print(np.logical_or([True, False], [False, False]))
# [ True False]
print(np.logical_or(x < 1, x > 3))
# [ True False False False True]
【例】计算x1 XOR x2的真值,按元素计算。
x = np.arange(5)
print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False True True False]
print(np.logical_xor(x < 1, x > 3))
# [ True False False False True]
print(np.logical_xor(0, np.eye(2)))
# [[ True False]
# [False True]]
对照
numpy.greater
numpy.greater_equal
numpy.equal
numpy.not_equal
numpy.less
numpy.less_equal
numpy.greater(x1, x2, *args, **kwargs) Return the truth value of (x1 > x2) element-wise.
numpy.greater_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 >= x2) element-wise.
numpy.equal(x1, x2, *args, **kwargs) Return (x1 == x2) element-wise.
numpy.not_equal(x1, x2, *args, **kwargs) Return (x1 != x2) element-wise.
numpy.less(x1, x2, *args, **kwargs) Return the truth value of (x1 < x2) element-wise.
numpy.less_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 =< x2) element-wise.
【例】numpy对以上对照函数进行了运算符的重载。
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 2
print(y)
print(np.greater(x, 2))
# [False False True True True True True True]
y = x >= 2
print(y)
print(np.greater_equal(x, 2))
# [False True True True True True True True]
y = x == 2
print(y)
print(np.equal(x, 2))
# [False True False False False False False False]
y = x != 2
print(y)
print(np.not_equal(x, 2))
# [ True False True True True True True True]
y = x < 2
print(y)
print(np.less(x, 2))
# [ True False False False False False False False]
y = x <= 2
print(y)
print(np.less_equal(x, 2))
# [ True True False False False False False False]
【例】
import numpy as np
np.random.seed(20200611)
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.random.randint(10, 40, [5, 5])
print(y)
# [[32 28 31 33 37]
# [23 37 37 30 29]
# [32 24 10 33 15]
# [27 17 10 36 16]
# [25 32 23 39 34]]
z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False False]
# [False False False False False]
# [False False True False True]
# [False True True False True]
# [ True False True False True]]
z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False False]
# [False False False False False]
# [False False True False True]
# [False True True False True]
# [ True True True False True]]
z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
# [False False False False False]
# [False False False False False]
# [False False False False False]
# [False True False False False]]
z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True True True True True]
# [ True True True True True]
# [ True True True True True]
# [ True True True True True]
# [ True False True True True]]
z = x < y
print(z)
print(np.less(x, y))
# [[ True True True True True]
# [ True True True True True]
# [ True True False True False]
# [ True False False True False]
# [False False False True False]]
z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True True True True True]
# [ True True True True True]
# [ True True False True False]
# [ True False False True False]
# [False True False True False]]
【例】注意 numpy 的广播规则。
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
np.random.seed(20200611)
y = np.random.randint(10, 50, 5)
print(y)
# [32 37 30 24 10]
z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False True]
# [False False False False True]
# [False False False False True]
# [False False False True True]
# [False False True True True]]
z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False True]
# [False False False False True]
# [False False False True True]
# [False False False True True]
# [False False True True True]]
z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
# [False False False False False]
# [False False False True False]
# [False False False False False]
# [False False False False False]]
z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True True True True True]
# [ True True True True True]
# [ True True True False True]
# [ True True True True True]
# [ True True True True True]]
z = x < y
print(z)
print(np.less(x, y))
# [[ True True True True False]
# [ True True True True False]
# [ True True True False False]
# [ True True True False False]
# [ True True False False False]]
z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True True True True False]
# [ True True True True False]
# [ True True True True False]
# [ True True True False False]
# [ True True False False False]]
numpy.isclose
numpy.allclose
numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns a boolean array where two arrays are element-wise equal within a tolerance.
numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns True if two arrays are element-wise equal within a tolerance.
numpy.allclose() 等价于 numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))。
The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
判断是否为True的计算依据:
np.absolute(a - b) <= (atol + rtol * absolute(b))
- atol:float,绝对公差。
- rtol:float,相对公差。
NaNs are treated as equal if they are in the same place and if equal_nan=True. Infs are treated as equal if they are in the same place and of the same sign in both arrays.
【例】比较两个数组是否可以认为相等。
import numpy as np
x = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x) # [ True False]
x = np.allclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x) # False
x = np.isclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x) # [ True True]
x = np.allclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x) # True
x = np.isclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x) # [False True]
x = np.allclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x) # False
x = np.isclose([1.0, np.nan], [1.0, np.nan])
print(x) # [ True False]
x = np.allclose([1.0, np.nan], [1.0, np.nan])
print(x) # False
x = np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x) # [ True True]
x = np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x) # True