3.18. Array Logic

3.18.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
import numpy as np


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

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

3.18.2. Value Comparison

3.18.2.1. Comparision with Scalar

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.18.2.2. Comparison with Array

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.18.3. Boolean Logic

3.18.3.1. Any

import numpy as np


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

any(a)
# True

a.any()
# True
import numpy as np


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

any(a)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

a.any()
# True

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

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

3.18.3.2. All

import numpy as np


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

all(a)
# False

a.all()
# False
import numpy as np


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

all(a)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

a.all()
# False

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

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

3.18.3.3. Logical NOT

import numpy as np


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

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


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

np.logical_not(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, False,  True],
#        [ True,  True,  True]])

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

3.18.3.4. 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])
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]])

3.18.3.5. 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])
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]])

3.18.4. Infinite

import numpy as np


a = np.array([1, 2, np.Inf])
# array([ 1.,  2., inf])

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

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


a = np.array([1, 2, np.Inf])
# array([ 1.,  2., inf])

np.isnan(a)
# array([False, False, False])

3.18.5. Not-a-Number

import numpy as np


a = np.array([1, 2, np.NaN])
# array([ 1.,  2., nan])

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


a = np.array([1, 2, np.NaN])
# array([ 1.,  2., nan])

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

np.isinf(a)
# array([False, False, False])

3.18.6. Assignments

3.18.6.1. Logic

  • Complexity level: easy

  • Lines of code to write: 4 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_logic.py

English
  1. Set random seed to zero

  2. Generate a: ndarray of 9 random integers from 0 to 100 (exclusive)

  3. Check for even numbers which are less than 50

  4. Check if all numbers matches this condition

  5. Check if any number matches this condition

Polish
  1. Ustaw ziarno losowości na zero

  2. Wygeneruj a: ndarray z 9 losowymi liczbami całkowitymi od 0 do 100 (rozłącznie)

  3. Sprawdź parzyste elementy, które są mniejsze od 50

  4. Sprawdź czy wszystkie liczby spełniają ten warunek

  5. Sprawdź czy jakakolwiek liczba spełnia ten warunek

3.18.6.2. Is in

  • Complexity level: easy

  • Lines of code to write: 9 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_isin.py

English
  1. Set random seed to zero

  2. Generate a: ndarray of 50 random integers from 0 to 100 (exclusive)

  3. Generate b: ndarray with sequential powers of 2 and exponential from 0 to 6 (inclusive)

  4. Check which elements from a are present in b

Polish
  1. Ustaw ziarno losowości na zero

  2. Wygeneruj a: ndarray z 50 losowymi liczbami całkowitymi od 0 do 100 (rozłącznie)

  3. Wygeneruj b: 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