3.5. Array Logic

3.5.1. Contains

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

2 in a
# True

0 in a
# False

[1, 2, 3] in a
# True

[1, 2] in a
# False

[3, 4] in a
# False

3.5.2. Is In

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

b = np.array([1, 5, 9])

np.isin(a, b)
# array([[ True, False, False],
#        [False,  True, False]])

3.5.3. Scalar Comparison

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a == 2
# array([[False,  True, False],
#        [False, False, False]])

a != 2
# array([[ True, False,  True],
#        [ True,  True,  True]])

a > 2
# array([[False, False,  True],
#        [ True,  True,  True]])

a >= 2
# array([[False,  True,  True],
#        [ True,  True,  True]])

a < 2
# array([[ True, False, False],
#        [False, False, False]])

a <= 2
# array([[ True,  True, False],
#        [False, False, False]])

3.5.4. Broadcasting Comparison

import numpy as np


a = np.array([1, 2, 3])
b = np.array([3, 2, 1])

a == b
# array([False, True, False])

a != b
# array([ True, False,  True])

a > b
# array([False, False,  True])

a >= b
# array([False,  True,  True])

a < b
# array([ True, False, False])

a <= b
# array([True, True, False])

3.5.5. Any

import numpy as np


a = np.array([True, False, False])
# array([True, False, False])

a.any()
# True
import numpy as np


a = np.array([[True, False, False],
              [True, True, True]])

a.any()
# True

a.any(axis=0)
# array([ True,  True,  True])

a.any(axis=1)
# array([ True,  True])

3.5.6. All

import numpy as np


a = np.array([True, False, False])

a.all()
# False
import numpy as np


a = np.array([[True, False, False],
              [True, True, True]])

a.all()
# False

a.all(axis=0)
# array([ True, False, False])

a.all(axis=1)
# array([False,  True])

3.5.7. Logical NOT

  • np.logical_not(...)

  • ~(...)

import numpy as np


a = np.array([[True, False, False],
              [True, True, True]])

np.logical_not(a)
# array([[False,  True,  True],
#        [False, False, False]])

~a
# array([[False,  True,  True],
#        [False, False, False]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.logical_not(a > 2)
# array([[ True,  True, False],
#        [False, False, False]])

~(a > 2)
# array([[ True,  True, False],
#        [False, False, False]])

3.5.8. Logical AND

  • Meets first and second condition at the same time

  • np.logical_and(..., ...)

  • (...) & (...)

import numpy as np


a = np.array([True, False, False])
b = np.array([True, True, False])

np.logical_and(a, b)
# array([ True, False, False])

a & b
# array([ True, False, False])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.logical_and(a > 2, a < 5)
# array([[False, False,  True],
#        [ True, False, False]])

(a > 2) & (a < 5)
# array([[False, False,  True],
#        [ True, False, False]])

3.5.9. Logical OR

  • Meets first or second condition at the same time

  • np.logical_or(..., ...)

  • (...) | (...)

import numpy as np


a = np.array([True, False, False])
b = np.array([True, True, False])

np.logical_or(a, b)
# array([ True,  True, False])

a | b
# array([ True,  True, False])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.logical_or(a < 2, a > 4)
# array([[ True, False, False],
#        [False,  True,  True]])

(a < 2) | (a > 4)
# array([[ True, False, False],
#        [False,  True,  True]])

3.5.10. Logical XOR

  • Meets first or second condition, but not both at the same time

  • np.logical_xor(..., ...)

  • (...) ^ (...)

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

np.logical_xor(a < 2, a > 4)
# array([[ True, False, False],
#        [False,  True,  True]])

(a < 2) ^ (a > 4)
# array([[ True, False, False],
#        [False,  True,  True]])

3.5.11. Readability Counts

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])


(a < 2) & (a > 4) | (a == 3)
# array([[False, False,  True],
#        [False, False, False]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

lower = (a > 2)
upper = (a < 6)
nine = (a == 9)
range = lower & upper

lower & upper
# array([[False, False,  True],
#        [ True,  True, False],
#        [False, False, False]])

range | nine
# array([[False, False,  True],
#        [ True,  True, False],
#        [False, False,  True]])

lower & upper | nine
# array([[False, False,  True],
#        [ True,  True, False],
#        [False, False,  True]])

3.5.12. Assignments

Code 3.46. Solution
"""
* Assignment: Numpy Logic Even
* Complexity: easy
* Lines of code: 3 lines
* Time: 5 min

English:
    1. Set random seed to zero
    3. Check for even numbers of `DATA` which are less than 50 and save result to `result`
    4. Check if all `result` matches this condition, result assing to `result_all`
    5. Check if any `result` matches this condition, result assign to `result_any`

Polish:
    1. Ustaw ziarno losowości na zero
    3. Sprawdź parzyste elementy `DATA`, które są mniejsze od 50 i wynik zapisz do `result`
    4. Sprawdź czy wszystkie `result` spełniają ten warunek, wynik zapisz do `result_all`
    5. Sprawdź czy jakakolwiek `result` spełnia ten warunek, wynik zapisz do `result_any`

    >>> type(result) is np.ndarray
    True
    >>> result
    array([ True, False, False, False, False, False, False, False,  True])
    >>> result_all
    False
    >>> result_any
    True
"""


# Given
import numpy as np
np.random.seed(0)

DATA = np.random.randint(0, 100, size=9)

result = ...
result_all = ...
result_any = ...


Code 3.47. Solution
"""
* Assignment: Numpy Logic Isin
* Complexity: easy
* Lines of code: 3 lines
* Time: 5 min

English:
    1. Set random seed to zero
    2. Generate `a: np.ndarray` of 50 random integers from 0 to 100 (exclusive)
    3. Generate `b: np.ndarray` with sequential powers of 2 and exponential from 0 to 6 (inclusive)
    4. Check which elements from `a` are present in `b`
    5. Result assign to `result`

Polish:
    1. Ustaw ziarno losowości na zero
    2. Wygeneruj `a: np.ndarray` z 50 losowymi liczbami całkowitymi od 0 do 100 (rozłącznie)
    3. Wygeneruj `b: np.ndarray` z kolejnymi potęgami liczby 2, wykładnik od 0 do 6 (włącznie)
    4. Sprawdź, które elementy z `a` są obecne w `b`
    5. Wynik przypisz do `result`

Tests:
    >>> type(result) is np.ndarray
    True
    >>> result
    array([False, False,  True, False, False, False, False, False, False,
           False, 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, False,
           False, False, False, False, False,  True, False, False, False,
            True, False, False, False, False])
"""


# Given
import numpy as np
np.random.seed(0)


result = ...