0% found this document useful (0 votes)
152 views

Comparisons, Masks, and Boolean Logic

This document discusses Boolean masks and comparisons in NumPy. It explains that Boolean masks allow examining and manipulating array values based on criteria. Comparison operators like <, > are implemented as element-wise universal functions in NumPy, returning Boolean arrays. These Boolean arrays can then be used as masks to select subsets of data, such as values less than a threshold. Examples demonstrate counting array entries that meet certain conditions and selecting subsets of data using Boolean masks.

Uploaded by

Ben Ten
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Comparisons, Masks, and Boolean Logic

This document discusses Boolean masks and comparisons in NumPy. It explains that Boolean masks allow examining and manipulating array values based on criteria. Comparison operators like <, > are implemented as element-wise universal functions in NumPy, returning Boolean arrays. These Boolean arrays can then be used as masks to select subsets of data, such as values less than a threshold. Examples demonstrate counting array entries that meet certain conditions and selecting subsets of data using Boolean masks.

Uploaded by

Ben Ten
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Comparisons, Masks, and

Boolean Logic
Numpy

PREPARED BY
R.AKILA,AP(SG)/CSE
BSACIST
REFERENCE:HTTPSPYTHON
D A T A S C I E N C E H A N D B O O K  B Y J A K E
VANDERPLAS
Comparisons, Masks, and Boolean Logic
Numpy

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

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.
Contd..

It is also possible to do an element-wise comparison


of two arrays, and to include compound expressions:
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 np.less(x, 3)
 A summary of the comparison operators and their
equivalent ufunc is shown here:
Just as in the case of arithmetic ufuncs, these will
work on arrays of any size and shape. Here is a two-
dimensional example:
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.
Counting entries
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, False is interpreted as 0, and True is
interpreted as 1:
Example: Counting Rainy Days

Imagine you have a series of data that represents the


amount of precipitation each day for a year in a given
city.
For example, here we'll load the daily rainfall
statistics for the city of Seattle in 2014, using Pandas.
Boolean Arrays as Masks

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
Using the Keywords and/or Versus the
Operators &/|
Fancy Indexing

Fancy indexing is like the simple indexing, but we


pass arrays of indices in place of single scalars. This
allows us to very quickly access and modify
complicated subsets of an array's values.

You might also like