1. Indexing NumPy arrays
In this lesson, you are going to learn how to use indexing to get data out of NumPy arrays and filter NumPy arrays to subsets of interest.
2. Indexing with ranges
Let's use this single dimensional array, arr, to explore how to extract data from NumPy arrays. We can see that this array has 100 elements.
Like in MATLAB, the colon indicates that you are selecting a range of indices. You can restrict the range further by specifying the beginning and end of the range. But this is where things work a bit differently from MATLAB.
On the left side of the colon, you specify the start of the range of indices to include. On the right side, you specify the terminating index, which will not be included in the slice.
In this example, we are selecting elements from the array arr, beginning with the index "10", indicating the eleventh element in the array and terminating with index "15".
3. Indexing multidimensional arrays
For multidimensional arrays, this same pattern can be continued with each additional dimension in the array. In this example, we have an image stored in the array image.
We can use positional indexing, first on the rows, then on the columns, to isolate a portion of the image and save it to the variable window. We separate the range used on each dimension with commas.
We can then print the shape of the window to confirm that this is a 50 by 100 array.
4. Indexing with Boolean arrays
In addition to indexing based on absolute positions and ranges, you can also use Boolean indexing to select rows or columns of an array based upon values in another Boolean array.
In this example, we have two arrays: data, which has 10000 elements, and is_valid, a Boolean array of the same shape which indicates whether the elements in the array data are valid or not. It has 8,732 True elements.
We can readily filter out the invalid entries in the data array by passing in the Boolean array through the square indexing brackets. This creates a new array, valid_data, which only includes the elements of data where is_valid was true.
We can then proceed with our analysis only applied to the valid data.
5. Let's practice!
Now that you know how to select data in NumPy arrays and summarize that data with NumPy functions, you're ready to practice what you've learned on some real data.