0% found this document useful (0 votes)
12 views5 pages

Boolean Arrays and Masks - Ipynb Colab

This document explains the use of Boolean masks in NumPy for examining and manipulating values within arrays, including counting and logical operations. It details comparison operators as universal functions (ufuncs) and how to utilize Boolean arrays for masking to select specific data subsets. Additionally, it clarifies the distinction between using logical keywords (and/or) versus bitwise operators (&/|) in Python.

Uploaded by

Ananthi.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Boolean Arrays and Masks - Ipynb Colab

This document explains the use of Boolean masks in NumPy for examining and manipulating values within arrays, including counting and logical operations. It details comparison operators as universal functions (ufuncs) and how to utilize Boolean arrays for masking to select specific data subsets. Additionally, it clarifies the distinction between using logical keywords (and/or) versus bitwise operators (&/|) in Python.

Uploaded by

Ananthi.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

 Comparisons, Masks, and Boolean

Logic
This section covers the use of Boolean masks to examine and manipulate values within NumPy
arrays. Masking comes up when you want to extract, modify, count, or otherwise manipulate
values in an array based on some criterion: for example, you might wish to count all values
greater than a certain value, or perhaps remove all outliers that are above some threshold. In
NumPy, Boolean masking is often the most efficient way to accomplish these types of tasks.

 Comparison Operators as ufuncs


In Computation on NumPy Arrays: Universal Functions we introduced ufuncs, and focused in
particular on arithmetic operators. We saw that using + , - , * , / , and others on arrays leads to
element-wise operations. NumPy also implements comparison operators such as < (less than) and
> (greater than) as element-wise ufuncs. The result of these comparison operators is always an
array with a Boolean data type. All six of the standard comparison operations are available:

i m p o r t numpy as np
x = np.array([1, 2, 3, 4, 5])

x < 4 # l e s s th an

a r r a y ( [ Tr u e , Tr u e , Tr u e , Fa l s e , F a l s e ] )

x > 3 # g r e a t e r th an

a r r a y ( [ F a l s e , F a l s e , Fa l s e , Tr u e , Tr u e ] )

x == 3 # equal

a r r a y ( [ F a l s e , Fa l s e , Tr u e , Fa l s e , F a l s e ] )

As in the case of arithmetic operators, the comparison operators are implemented as ufuncs in
NumPy; for example, when you write x < 3 , internally NumPy uses n p . l e s s ( x , 3 ) . A summary
of the comparison operators and their equivalent ufunc is shown here:
Operat Equivalent Operato Equivalent
or ufunc r ufunc
== n p . e q ua l != np.not_equal

< np.less <= np.less_equal

> np.greater >= np.greater_equ


al

Just as in the case of arithmetic ufuncs, these will work on arrays of any size and shape. Here is
a two-dimensional example:

i m p o r t numpy as np
x=np.array([[1,2],
[3,4]]) x

array([[1, 2],
[3, 4]])
x >10

a r r a y ( [ [ Fa l s e , Fa l s e ] ,
[ Fa l s e , Fa l s e ] ] )

In each case, the result is a Boolean array, and NumPy provides a number of straightforward
patterns for working with these Boolean results.

 Working with Boolean Arrays


Given a Boolean array, there are a host of useful operations you can do. We'll work with x , the
two-dimensional array we created earlier.

print(x)

[ [ 1 2]
[3 4]]

 Counting
entries
To count the number of True entries in a Boolean array, np .count_ nonzero is useful:

# how many v a l u e s l e s s
t h a n 5?
np.count_nonzero(x < 5)

We see that there are eight


array entries that are less
than 6. Another way to get at
this information is to use
np.sum ; in this case, Fa l s e
is interpreted as 0 , and
True is interpreted as 1 :

np.sum(x<

2) 1

array([[1
, 2],
[3,
4
]
]
)

# a r e t h e r e any v a l u e s g r e a t e r
t h a n 8? n p . a n y ( x > 8 )

Fa l s e

# a r e t h e r e any v a l u e s l e s s
than zero? np.any(x < 0)

Fa l s e
# are a l l values less
t h a n 5? n p . a l l ( x < 5 )

Tru e

# are a l l values equal


t o 6? n p . a l l ( x == 6 )

Fa l s e

 Boolean
operators
We've already seen how we might count, say, all days with rain less than four inches, or all days
with rain greater than two inches. But what if we want to know about all days with rain less than
four inches and greater than one inch?
This is accomplished through Python's bitwise logic operators, & , | , ^ , and ~ . Like with the
standard arithmetic operators, NumPy overloads these as ufuncs which work element-wise on
(usually Boolean) arrays.

n p . s u m ( (x > 0 . 5 ) & ( x

< 3)) 2

Combining comparison operators and Boolean operators on arrays can lead to a wide range of
efficient logical operations.
The
r
following
Operato table summarizes
Equivalent
ufunc
the bitwise Boolean operators and their equivalent ufuncs:
Operato Equivalent
r ufunc
& np.bitwise_and | np.bitwise_or

^ np.bitwise_xor ~ np.bitwise_not

 Boolean Arrays as Masks


In the preceding section we looked at aggregates computed directly on Boolean arrays. A more
powerful pattern is to use Boolean arrays as masks, to select particular subsets of the data
themselves. Returning to our x array from before, suppose we want an array of all values in the
array that are less than, say, 5:

array([[1, 2],
[3, 4]])

We can obtain a Boolean array for


this condition easily, as we've
already seen:

x < 3

a r r a y ( [ [ Tr u e , Tr u e ] ,
[ Fa l s e , Fa l s e ] ] )

Now to select these values from


the array, we can simply index on
this Boolean array; this is known
as a masking
array([1, 2])

What is returned is a one-dimensional array filled with all the values that meet this condition; in
other words, all the values in positions at which the mask array is True .
We are then free to operate on these values as we wish.

By combining Boolean operations, masking operations, and aggregates, we can very quickly
answer these sorts of questions for our dataset.

 Aside: Using the Keywords and/or Versus the Operators &/|


One common point of confusion is the difference between the keywords and and o r on
one hand, and the operators & and | on the other hand. When would you use one versus
the other?
The difference is this: and and o r gauge the truth or falsehood of entire object, while & and |
refer to bits within each object.
When you use and or o r , it's equivalent to asking Python to treat the object as a single Boolean
entity. In Python, all nonzero integers will evaluate as True. Thus:

bool(42),

bool(0)

( Tr u e ,

Fa l s e )

b o o l ( 4 2 and 0 )

Fa l s e

bool(42 o r

0 ) Tru e

When you use & and | on integers, the expression operates on the bits of the element, applying the
and or the or to the individual bits making up the number:

b i n ( 4 2 & 59)

' 0b 101010'

bin(42 | 59)

' 0b 111011'

You might also like