835a42b07b
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> |
||
---|---|---|
.. | ||
README.md |
Boolean matricies and logic functions
{:.no_toc}
* TOC {:toc}The goal
Questions to David Rotermund
Boolean matrices
There are different ways to get a Boolean matrix. For example the result of a np.isfinite() (checks if the values in a matrix are infite values) is a Boolean matrix.
import numpy as np
a = 1.0 / np.arange(0, 6).reshape((2, 3))
print(a)
print()
print(np.isfinite(a))
Output:
[[ inf 1. 0.5 ]
[0.33333333 0.25 0.2 ]]
[[False True True]
[ True True True]]
<ipython-input-3-7ead4ee291d3>:4: RuntimeWarning: divide by zero encountered in divide
a = 1.0 / np.arange(0, 6).reshape((2, 3))
However, there are other ways to produce Boolean matrixes:
import numpy as np
a = np.arange(0, 6).reshape((2, 3))
print(a)
print()
print((a > 1))
Output:
[[0 1 2]
[3 4 5]]
[[False False True]
[ True True True]]
Calculating with boolean matrices
You can treat them a matricies that are filled with 0 (False) and 1 (True):
import numpy as np
a = np.arange(0, 6).reshape((2, 3))
print(a)
print()
print((a > 1) * (a < 4))
Output:
[[0 1 2]
[3 4 5]]
[[False False True]
[ True False False]]
Or you can use Boolean logical matrix functions:
import numpy as np
a = np.arange(0, 6).reshape((2, 3))
print(a)
print()
print(np.logical_and((a > 1), (a < 4)))
Output:
[[0 1 2]
[3 4 5]]
[[False False True]
[ True False False]]
np.nonzero()
numpy.nonzero(a)
Return the indices of the elements that are non-zero.
We can use nonzero for extracting the positions where the boolean value is true:
import numpy as np
a = np.arange(0, 6).reshape((2, 3))
print(a)
print()
print(np.nonzero(a > 1))
Output:
[[0 1 2]
[3 4 5]]
(array([0, 1, 1, 1]), array([2, 0, 1, 2]))
For reversing it, you can use this:
import numpy as np
a = np.arange(0, 6).reshape((2, 3))
a = (a > 1)
print(a)
idx = np.nonzero(a)
print()
b = np.zeros((2, 3), dtype=bool)
b[idx] = True
print(b)
Output:
[[False False True]
[ True True True]]
[[False False True]
[ True True True]]
Logic functions
Truth value testing
all(a[, axis, out, keepdims, where]) | Test whether all array elements along a given axis evaluate to True. |
any(a[, axis, out, keepdims, where]) | Test whether any array element along a given axis evaluates to True. |
Array contents
isfinite(x, /[, out, where, casting, order, ...]) | Test element-wise for finiteness (not infinity and not Not a Number). |
isinf(x, /[, out, where, casting, order, ...]) | Test element-wise for positive or negative infinity. |
isnan(x, /[, out, where, casting, order, ...]) | Test element-wise for NaN and return result as a boolean array. |
isnat(x, /[, out, where, casting, order, ...]) | Test element-wise for NaT (not a time) and return result as a boolean array. |
isneginf(x[, out]) | Test element-wise for negative infinity, return result as bool array. |
isposinf(x[, out]) | Test element-wise for positive infinity, return result as bool array. |
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) | Check 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(element) | Returns True if the type of element is a scalar type. |
Logical operations
logical_and(x1, x2, /[, out, where, ...]) | Compute the truth value of x1 AND x2 element-wise. |
logical_or(x1, x2, /[, out, where, casting, ...]) | Compute the truth value of x1 OR x2 element-wise. |
logical_not(x, /[, out, where, casting, ...]) | Compute the truth value of NOT x element-wise. |
logical_xor(x1, x2, /[, out, where, ...]) | Compute the truth value of x1 XOR x2, element-wise. |
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[, equal_nan]) | 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, where, casting, ...]) | Return the truth value of (x1 > x2) element-wise. |
greater_equal(x1, x2, /[, out, where, ...]) | Return the truth value of (x1 >= x2) element-wise. |
less(x1, x2, /[, out, where, casting, ...]) | Return the truth value of (x1 < x2) element-wise. |
less_equal(x1, x2, /[, out, where, casting, ...]) | Return the truth value of (x1 <= x2) element-wise. |
equal(x1, x2, /[, out, where, casting, ...]) | Return (x1 == x2) element-wise. |
not_equal(x1, x2, /[, out, where, casting, ...]) | Return (x1 != x2) element-wise. |