Professional Documents
Culture Documents
Lec 05-07 Ch2 Numpy Part 2
Lec 05-07 Ch2 Numpy Part 2
NUMPY
Disclaimer Statement
In preparation of these slides, materials have been taken from different online sources in the
shape of books, websites, research papers and presentations etc. However, the author does not
have any intention to take any benefit of these in her/his own name. This lecture (audio, video,
slides etc) is prepared and delivered only for educational purposes and is not intended to
infringe upon the copyrighted material. Sources have been acknowledged where applicable. The
views expressed are presenter’s alone and do not necessarily represent actual author(s) or the
institution.
Computation on Arrays
: Broadcasting
Broadcasting
The term broadcasting describes how numpy treats arrays with different shapes during
arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across
the larger array so that they have compatible shapes.
Broadcasting
Broadcasting is simply a set of rules for applying binary ufuncs (addition, subtraction,
multiplication, etc.) on arrays of different sizes.
Broadcasting solves the problem of arithmetic between arrays of differing shapes by in effect
replicating the smaller array along the last mismatched dimension.
NumPy does not actually duplicate the smaller array; instead, it makes memory and
computationally efficient use of existing structures in memory that in effect achieve the same
result.
Add a one-dimensional array to a two-dimensional array:
Visualization of NumPy broadcasting
Rules of Broadcasting
Broadcasting in NumPy follows a strict set of rules to determine the
interaction between the two arrays:
1. If the two arrays differ in their number of dimensions, the shape of the
one with fewer dimensions is padded with ones on its leading (left) side.
2. If the shape of the two arrays does not match in any dimension, the
array with shape equal to 1 in that dimension is stretched to match the
other shape.
3. If in any dimension the sizes disagree and neither is equal to 1, an error is
raised.
Example: Add a two-dimensional array to a
one-dimensional array
An example in which the two arrays are not compatible:
• Now we hit rule 3—the final shapes do not match, so these two arrays are incompatible, as we
can observe by attempting this operation:
Comparisons, Masks, and Boolean
Logic
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.
Comparison Operators as ufuncs
Two-dimensional example:
Working with Boolean Arrays
Working with Boolean Arrays
Counting entries
• To count the number of True entries in a Boolean array
• counts the number of values less than 6 in each row of the matrix.
Working with Boolean Arrays
Boolean Arrays as Masks
The difference is this: and or gauge the truth or falsehood of entire object, while & and | refer to bits
within each object.
Fancy Indexing
Fancy indexing is like the simple indexing we’ve already seen, 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.
Exploring Fancy Indexing
Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array
elements at once
Exploring Fancy Indexing
• Suppose we want to access three different elements
• Like with standard indexing, the first index refers to the row, and the second to the column:
Combined Indexing
• np.sort:
Fast Sorting in NumPy: np.sort and np.argsort
• np.argsort
Sorting along rows or columns
Sorting along rows or columns
Partial Sorts: Partitioning