Filtering with Boolean Array

Posted under » Python Data Analysis on 22 May 2024

Booleans are just 0 or 1. It look like this in pandas.

>>> mask = songs3 > songs3.median ()

>>> mask
Paul True
John True
George False
Ringo False
Name: counts , dtype: bool

In Numpy we can do it in multiple dimensions.

import numpy as np 

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

c = np.array_equal(a, b)
print(c)

False

print(a == b)

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

If you have a matching boolean array, then you will get true.

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

d = (a == b)

print(np.array_equal(d, f))

True

Sometimes, you need not be 100% equal, you can use either sum or np.count_nonzero to adjust the percentage of `equalness'.

>>> import numpy as np
>>> f = np.array([[1, 1], [1, 0], [0, 1]], dtype=bool)
>>> f
array([[ True  True]
 [ True False]
 [False  True]])

>>> f.sum()
4

>>> np.count_nonzero(f)
4

If you need to be equal, then you can Slice the array.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data