1. Indexing and slicing arrays
Now that we can create arrays, let's learn to access and sort the data inside them.
2. Indexing 1D arrays
Indexing is an order-based method for accessing data. NumPy indexing is zero-based, meaning the first index is index zero. Array indexing uses square brackets, similar to indexing Python lists.
3. Indexing elements in 2D
When indexing a 2D array, give NumPy both a row and column index in order to return a single element. For example, to get the value in the third row of the fifth column, we provide both the row index, two, and the column index, four. Here, like everywhere else in NumPy, row information comes before column information.
4. Indexing rows in 2D
If we give NumPy just one index when indexing a 2D array, it assumes that the index is a row index. For example, asking for index 0 will return the first row.
5. Indexing columns in 2D
To index a column, indicate a column index by providing a colon in place of any row index. The colon by itself tells NumPy that we are looking for all row information. In this case, we want all row data in the fourth column, at column index three.
6. Slicing 1D arrays
Slicing extracts a subset of data based on given indices from one array and creates a new array with the sliced data. To slice, provide a start and stop value, separated by a colon and enclosed in square brackets. The element at the start index is included in the result, but the one at the stop index is not: here, the number 10 at index four is excluded from the result.
7. Slicing 2D arrays
To slice in 2D, we'll need to give NumPy information on how both the rows and columns should be sliced. Row start and stop indices will be followed by column start and stop indices, separated by a comma. Here, we select the intersection of the third, fourth, and fifth rows and columns from our sudoku game.
8. Slicing with steps
In addition to start and stop values, we can give NumPy a third number: step value. For example, to slice only the corner values of the middle sudoku square, we indicate that we are only looking for every other value by listing a step size of two.
Here, we are stepping in both rows and columns. We can also step only row or column values by leaving out the step value on one or the other.
9. Sorting arrays
The np-dot-sort function sorts an array along a given axis. Here, NumPy has sorted the array along the columns, with the highest number from each row in the far right. What if we want to sort by row? To do that, we need to understand NumPy axis labels.
10. Axis order
In a 2D array, the direction along rows is axis zero. The direction along columns is axis one.
An easy way to remember that axis one refers to columns is that a column looks like the number one!
11. Sorting by axis
The default axis in np-dot-sort is the last axis of the array passed to it. If a 2D array is being sorted, NumPy sorts by columns, since columns are axis 1 and rows are axis 0. This is why the array we saw earlier, replicated here, is sorted so that the highest numbers from each row are in the far-right column.
To sort the array by row, so that the highest numbers in each column are at the bottom of the array, set the axis keyword argument to zero. axis is a very common keyword argument because many NumPy functions can be performed across any axis!
12. Let's practice!
Time to show off your indexing and slicing by chopping into some tree data!