Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

NumPy Logo

Overview

This chapter will introduce some of NumPy’s more advanced capabilities, with a focus on using indexing operations to manipulate arrays of numbers. We’ll look first at basic ordered indexing of arrays with multiple dimensions and some vector operations. Then we’ll look at subsetting arrays using boolean logic and finally using array indexing operations for sorting and subsetting.

Prerequisites

ConceptsImportanceNotes
NumPy BasicsNecessaryHarris et al. (2020)

Imports

We will be including Matplotlib to illustrate some of our examples, but you don’t need knowledge of it to complete this notebook.

Using axes to slice arrays

Here we introduce an important concept when working with NumPy: the axis. This indicates the particular dimension along which a function should operate (provided the function does something taking multiple values and converts to a single value).

Let’s look at a concrete example with sum:

This calculates the total of all values in the array.

Now, with a reminder about how our array is shaped,

we can specify axis to get just the sum across each of our rows.

Or do the same and take the sum across columns:

After putting together some data and introducing some more advanced calculations, let’s demonstrate a multi-layered example: calculating temperature advection. If you’re not familiar with this (don’t worry!), we’ll be looking to calculate

advection=−v⃗⋅∇T\text{advection} = -\vec{v} \cdot \nabla T

and to do so we’ll start with some random TT and v⃗\vec{v} values,

We can calculate the np.gradient of our new T(100x50)T(100x50) field as two separate component gradients,

In order to calculate −v⃗⋅∇T-\vec{v} \cdot \nabla T, we will use np.dstack to turn our two separate component gradient fields into one multidimensional field containing xx and yy gradients at each of our 100x50100x50 points,

and then do the same for our separate uu and vv wind components,

Finally, we can calculate the dot product of these two multidimensional fields of wind and temperature gradient components by hand as an element-wise multiplication, *, and then a sum of our separate components at each point (i.e., along the last axis),

Indexing arrays with boolean values

Array comparisons

NumPy can easily create arrays of boolean values and use those to select certain values to extract from an array

By doing a comparison between a NumPy array and a value, we get an array of values representing the results of the comparison between each element and the value

This, which is its own NumPy array of boolean values, can be used as an index to another array of the same size. We can even use it as an index within the original temp array we used to compare,

If we store this array somewhere new,

We find that our original (100,) shape array is too large to subset our new (60,) array.

If their sizes do match, the boolean array can come from a totally different array!

Replacing values

To extend this, we can use this conditional indexing to assign new values to certain positions within our array, somewhat like a masking operation.

and to put this in context,

If we use parentheses to preserve the order of operations, we can combine these conditions with other bitwise operators like the & for bitwise_and,

Heat index is only defined for temperatures >= 80F and relative humidity values >= 40%. Using the data generated below, we can use boolean indexing to extract the data where heat index has a valid value.

Another bitwise operator we can find helpful is Python’s ~ complement operator, which can give us the inverse of our specific mask to let us assign np.nan to every value not satisfied in good_heat_index.

Indexing using arrays of indices

You can also use a list or array of indices to extract particular values--this is a natural extension of the regular indexing. For instance, just as we can select the first element:

We can also extract the first, fifth, and tenth elements as a list:

One of the ways this comes into play is trying to sort NumPy arrays using argsort. This function returns the indices of the array that give the items in sorted order. So for our temp,

i.e., our lowest value is at index 52, next 57, and so on. We can use this array of indices as an index for temp,

to get a sorted array back!

With some clever slicing, we can pull out the last 10, or 10 highest, values of temp,

There are other NumPy arg functions that return indices for operating; check out the NumPy docs on sorting your arrays!


Summary

In this notebook we introduced the power of understanding the dimensions of our data by specifying math along axis, used True and False values to subset our data according to conditions, and used lists of positions within our array to sort our data.

What’s next

Taking some time to practice this is valuable to be able to quickly manipulate arrays of information in useful or scientific ways.

Additional resources

The NumPy Users Guide expands further on some of these topics, as well as suggests various Tutorials, lectures, and more at this stage.

References
  1. Harris, C. R., Millman, K. J., van der Walt, S. J., Gommers, R., Virtanen, P., Cournapeau, D., Wieser, E., Taylor, J., Berg, S., Smith, N. J., Kern, R., Picus, M., Hoyer, S., van Kerkwijk, M. H., Brett, M., Haldane, A., del Río, J. F., Wiebe, M., Peterson, P., … Oliphant, T. E. (2020). Array programming with NumPy. Nature, 585(7825), 357–362. 10.1038/s41586-020-2649-2