Unit 26Understanding Conditional Functions

The function where(c, a, b) is the numpy idea of the ternary operator if-else. It takes a Boolean array (c) and two other arrays (a and b) and returns array d[i]=a[i] if c[i] else b[i]. All three arrays must be of the same shape.

The functions any and all return True if any or all array elements are True, respectively.

The function nonzero returns the indexes of all non-zero elements.

In Unit 25, Demystifying Universal Functions, we recorded some S&P stock prices in an array sap. To find out which stock changed substantially (by more than $1.00 per share), let’s replace “small” changes with zeros, locate the non-zero elements, and use their indexes as a “smart index” into the array of stock ...

Get Data Science Essentials in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.