Advanced Array Indexing¶
Earlier we discussed basic array indexing techniques. Here we discuss advanced techniques and take a deeper dive into how array indexing works.
We start by setting up a 3x2x4 array of integers, foo
.
Observe the result of foo[:,:,0]
Recall the mental model for interpreting an N-dimensional array.
- 1-d array: imagine a row of values
- 2-d array: imagine a matrix of values
- 3-d array: imagine a row of matrices
- 4-d array: imagine a matrix of matrices
In this case, we can think of foo
as a row of matrices where element (i,j,k)
corresponds to the ith matrix, the jth row, and the kth column. So, foo[:,:,0]
is requesting "every matrix, every row, column 0" resulting in this sub-array.
What's surprising is that we started with a 3-dimensional array, but foo[:,:,0]
returned a 2-dimensional array. When we select more than one column, for example like this
the result is a 3-dimensional array as you might expect.
Subsetting with same-shape index arrays¶
Let's see another example, this time using 3x2 index arrays for i, j, and k indexes of foo
.
Important
When every dimension is indexed with an array (or list) and each of those arrays is the same shape, the output array will be the same shape as the index arrays.
In this case each of our index arrays is 3x2, so we know our result will also be a 3x2 array.
To understand the values in the result, you could imagine zipping the three index arrays into a matrix of 3-element tuples like this
Here, each tuple gives the location of the corresponding output element.
Here's another example where we use 2x2x2x2 index arrays to extract values from foo
. Notice the result is also 2x2x2x2.
Again, the shape of the output is dependent on the shape of the index arrays, not the shape of the array you're indexing into.
Subsetting with varying-shape index arrays¶
We've considered the case where every dimension has an index array and each of the index arrays are the same shape. What if every dimension has an index array, but the index arrays have different shapes?
In this case, NumPy broadcasts the index arrays, in essence forming three 2x3 index arrays.
Tip
You can do np.broadcast_arrays(i,j,k)
to see how the original index arrays broadcast into identically shaped index arrays. See broadcast_arrays()
.
Subsetting foo
with equivalently shaped index arrays is straight-forward based on what we already covered.
Subsetting with sice indexers¶
Lastly, we consider the case where our array contains slice indexers :
. For example,
What's going on here? The trick to understanding these complex scenarios is to build fully expanded, same-shaped index arrays for each dimension as follows:
Let's walk through that last example. We start by broadcasting the index arrays into the following (3,4) arrays.
Now we create an index array for j. Start with a (3,4) array of ?
s.
Since j spans the second axis of foo
which is length-2, replace each ?
with the indices [0,1]
.
j is then promoted from a (3,4) array to a (3,4,2) array.
i and k are then each copied twice along a new last axis to match j's shape. The final (3,4,2) index arrays are
Note
NumPy doesn't actually expand the index arrays in this way as it'd be innefficient, but NumPy's indexing algorithm is logically equivalent to this type of index array expansion.
Note
The slicer we used in this example :
is a full slice but we could use fancier slices like ::2
which gets every second element or :3:-1
which gets the first three elements in reverse order.
Trailing Indexes¶
It's also worth mentioning that you can exclude trailing indexes in which case NumPy assumes you want all the values
from those excluded dimensions. For example, foo[[0,1]]
is equivalent to foo[[0,1], :, :]
.
Indexing with a scalar¶
Recall foo[:,:,0]
returns a two-dimensional array even though foo
is a three-dimensional array. Why?
Recall the algorithm for determining the output of a subset with slicers and index arrays.
We start by broadcasting all the index arrays. In this case there's only one index array - 0 - so we don't have to do any broadcasting, but what's the shape of a scalar like 0? It's actually empty or 0-dimensional. You can see this by calling
So we have an empty dimensional index, (). Then we concatenate the size of each dimension with a slice, so we end up with a (3,2) result.
If we had wrapped the 0 index in square brackets, the final result would be a (3,2,1) array.
View vs Copy¶
It's important to understand when array indexing produces a view and when it produces a copy.
Suppose we have this 2d array, spongebob
We can pick out the first two columns using array indexers or slicing.
On the surface these techniques seem to produce the same result
but under the hood, these techniques are subtly different. squidward
is a copy of spongebob
while patrick
is a view of spongebob
.
Observe that if we modify squidward
, spongebob
is unchanged.
By contrast, if we modify patrick
, spongebob
changes too.
Array memory addresses¶
Another way to observe this is by looking up the memory address of the beginning of each array.
It's clear that spongebob
and patrick
live at the same memory address, but the location of squidward
is elsewhere.
Warning
The memory addresses listed here are the address of the beginning of each array. If two addresses are different, the arrays don't start at the same place, but portions of the arrays may still overlap!
A better technique to check if arrays share memory is to use NumPy's shares_memory()
function.
How to force copy an array¶
In, general, when you subset an array using nothing but slices, NumPy returns a view of the original array. You can force numpy to copy the data using the array copy()
method like this.
Note
When you subset an array using at least one index array, numpy will automatically copy the data.