Table Of Contents
- NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
- Intermediate Array Stuff
3.4 boolean indexing
- Common Operations
4.2 Math Funcs
4.3 all and any
This video explains the key concepts behind math functions in NumPy by taking an in-depth look at the sum() function. We’ll see how to calculate row sums, column sums, and sums with nan.
import numpy as np squee = np.array( [[5.0, 2.0, 9.0], [1.0, 0.0, 2.0], [1.0, 7.0, 8.0]] ) # sum np.sum(squee) # 35.0 # sum over rows, columns np.sum(squee, axis = 0) # sum across axis 0 (column sums) np.sum(squee, axis = 1) # sum across axis 1 (row sums) # keepdims np.sum(squee, axis = 0, keepdims=True) # [[ 7., 9., 19.]] # sum with nans squee[0, 0] = np.nan np.sum(squee) # sum across axis 1 (row sums) nan # sum excluding nans np.sum(squee, where = ~np.isnan(squee)) # 30.0 # or.. np.nansum(squee) # 30.0
Numpy provides a variety of math functions like sum, mean, min, max, floor, round, exp, log, and countless others. When you understand how to use one of these functions, you’ll understand how to use nearly all of them since they have similar interfaces and behavior.
Here we have a 2d array called squee. That’s what my girlfriend calls squirrels.
Let’s take a look at the sum function.
If we call np.sum(squee) with no other parameters, we get back 35, the sum of all elements in the array.
We can set axis = 0 to sum across axis 0, in other words, calculating column sums.
And we can set axis = 1 to sum across the columns, calculating row sums.
In both of these cases, numpy collapses our 2d array into a 1d array. If you want to prevent this behavior, you can set keepdims = True.
Now, let’s see what happens if squee contains nan values.
If numpy encounters a nan value in its calculation, it’ll return nan. If you want to ignore nan values, you can use the where argument to exclude them. With the where argument, you pass in a boolean array which can be broadcasted to match the dimensions of squee, telling numpy which elements you want to sum. So, if we want to exclude nans, we just need to pass in “where = negation np.isnan squee” Or, just us the nansum() function which treats every nan value as 0.