In [1]:
import numpy as np

In [2]:
a = np.arange(12).reshape(3, 4)
print(a)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [3]:
b = np.arange(12).reshape(4, 3).T
print(b)


[[ 0  3  6  9]
 [ 1  4  7 10]
 [ 2  5  8 11]]

In [4]:
a_compare = a < b
print(a_compare)


[[False  True  True  True]
 [False False  True  True]
 [False False False False]]

In [5]:
print(type(a_compare))


<class 'numpy.ndarray'>

In [6]:
print(a_compare.dtype)


bool

In [7]:
print(a > b)


[[False False False False]
 [ True  True False False]
 [ True  True  True False]]

In [8]:
print(a <= b)


[[ True  True  True  True]
 [False False  True  True]
 [False False False  True]]

In [9]:
print(a >= b)


[[ True False False False]
 [ True  True False False]
 [ True  True  True  True]]

In [10]:
print(a == b)


[[ True False False False]
 [False False False False]
 [False False False  True]]

In [11]:
print(a != b)


[[False  True  True  True]
 [ True  True  True  True]
 [ True  True  True False]]

In [12]:
b_float = b.astype(float)
print(b_float)


[[ 0.  3.  6.  9.]
 [ 1.  4.  7. 10.]
 [ 2.  5.  8. 11.]]

In [13]:
print(b_float.dtype)


float64

In [14]:
print(a == b_float)


[[ True False False False]
 [False False False False]
 [False False False  True]]

In [15]:
b_1d = np.arange(4, 8)
print(b_1d)


[4 5 6 7]

In [16]:
print(a < b_1d)


[[ True  True  True  True]
 [False False False False]
 [False False False False]]

In [17]:
print(a < 6)


[[ True  True  True  True]
 [ True  True False False]
 [False False False False]]

In [18]:
print(a % 2)


[[0 1 0 1]
 [0 1 0 1]
 [0 1 0 1]]

In [19]:
print(a % 2 == 0)


[[ True False  True False]
 [ True False  True False]
 [ True False  True False]]

In [20]:
print(np.count_nonzero(a < 6))


6

In [21]:
print(np.all(a < 6))


False

In [22]:
print(np.all(a < 6, axis=1))


[ True False False]

In [23]:
print(np.any(a < 6))


True

In [24]:
print(np.any(a < 6, axis=1))


[ True  True False]

In [25]:
a_nan = np.array([0, 1, np.nan])
print(a_nan)


[ 0.  1. nan]

In [26]:
print(a_nan == np.nan)


[False False False]

In [27]:
print(np.isnan(a_nan))


[False False  True]

In [28]:
print(a_nan > 0)


[False  True False]
/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in greater
  """Entry point for launching an IPython kernel.

In [29]:
a_nan_only = np.array([np.nan])
print(a_nan_only)


[nan]

In [30]:
print(a_nan_only > 0)


[False]

In [31]:
print(np.nan > 0)


False

In [32]:
x = 6

In [33]:
print(4 < x < 8)


True

In [34]:
# print(4 < a < 8)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [35]:
print((a > 4) & (a < 8))


[[False False False False]
 [False  True  True  True]
 [False False False False]]

In [36]:
# print((a > 4) and (a < 8))
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [37]:
# print(a > 4 & a < 8)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [38]:
x = 6
y = 6
z = 6

In [39]:
print(x == y == z)


True

In [40]:
c = np.zeros((3, 4), int)
print(c)


[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

In [41]:
# print(a == b == c)
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [42]:
print((a == b) & (b == c))


[[ True False False False]
 [False False False False]
 [False False False False]]