numpy教程:逻辑函数Logic functions

时间:2023-02-04 10:15:04

http://blog.csdn.net/pipisorry/article/details/48208433

真值测试Truth value testing

all(a[, axis, out, keepdims]) Test whether all array elements along a given axis evaluate to True.
any(a[, axis, out, keepdims]) Test whether any array element along a given axis evaluates to True.

只要数组中有一个值为True,则any()返回True;而只有数组的全部元素都为True,all()才返回True。

也可以直接当成array数组的方法使用。

判断numpy数组是否为空

if a.size:
 print('array is not empty')

如果通过python列表,把一个列表作为一个布尔值会产生True如果有项目,False如果它是空的。
lst = []
if lst:
 print "array has items"
if not lst:
 print "array is empty"

[Python的-如何检查数组不为空?]

判断numpy数组中是否有True

array.any()

皮皮blog

数组内容Array contents

isfinite(x[, out]) Test element-wise for finiteness (not infinity or not Not a Number).
isinf(x[, out]) Test element-wise for positive or negative infinity.
isnan(x[, out]) Test element-wise for NaN and return result as a boolean array.
isneginf(x[, y]) Test element-wise for negative infinity, return result as bool array.
isposinf(x[, y]) Test element-wise for positive infinity, return result as bool array.

numpy.isnan

numpy判断一个元素是否为np.NaN,判断某元素是否是nan

numpy.isnan(element)

Note: 不能使用array[0] == np.NaN,总是返回False!

numpy数组元素替换numpy.nan_to_num(x)

判断某元素是否是nan,inf,neginf,如果是,nan换为0,inf换为一个非常大的数,neginf换为非常小的数

numpy.nan_to_num(x)
Replace nan with zero and inf with finite numbers.
Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number.

数组类型测试Array type testing

iscomplex(x) Returns a bool array, where True if input element is complex.
iscomplexobj(x) Check for a complex type or an array of complex numbers.
isfortran(a) Returns True if the array is Fortran contiguous but not C contiguous.
isreal(x) Returns a bool array, where True if input element is real.
isrealobj(x) Return True if x is a not complex type or an array of complex numbers.
isscalar(num) Returns True if the type of num is a scalar type.

逻辑操作Logical operations

logical_and(x1, x2[, out]) Compute the truth value of x1 AND x2 element-wise.
logical_or(x1, x2[, out]) Compute the truth value of x1 OR x2 element-wise.
logical_not(x[, out]) Compute the truth value of NOT x element-wise.
logical_xor(x1, x2[, out]) Compute the truth value of x1 XOR x2, element-wise.

两个0-1array相与操作

判断两个0-1array有多少个相同的1, 两种方式

rate = np.count_nonzero(np.logical_and(fs_predict_array, ground_truth_array))
rate = np.count_nonzero(fs_predict_array * ground_truth_array)

不过fs_predict_array * ground_truth_array返回的是0-1array,而np.logical_and(fs_predict_array ,ground_truth_array)返回的是True-False array,但是都可以使用sum()得到1或者True的数目。

lz亲测下面的logical_and操作运行速度更快,没有count_nonzero会更快。

皮皮blog

比较Comparison

allclose(a, b[, rtol, atol, equal_nan]) Returns True if two arrays are element-wise equal within a tolerance.
isclose(a, b[, rtol, atol, equal_nan]) Returns a boolean array where two arrays are element-wise equal within a tolerance.
array_equal(a1, a2) True if two arrays have the same shape and elements, False otherwise.
array_equiv(a1, a2) Returns True if input arrays are shape consistent and all elements equal.
greater(x1, x2[, out]) Return the truth value of (x1 > x2) element-wise.
greater_equal(x1, x2[, out]) Return the truth value of (x1 >= x2) element-wise.
less(x1, x2[, out]) Return the truth value of (x1 < x2) element-wise.
less_equal(x1, x2[, out]) Return the truth value of (x1 =< x2) element-wise.
equal(x1, x2[, out]) Return (x1 == x2) element-wise.
not_equal(x1, x2[, out]) Return (x1 != x2) element-wise.

allclose

如果两个数组在tolerance误差范围内相等,则返回True。

from: http://blog.csdn.net/pipisorry/article/details/48208433

ref: Logic functions