|
| 1 | +## Integer array indexing |
| 2 | + |
| 3 | +NumPy arrays may be indexed with other arrays. |
| 4 | +For all cases of index arrays, what is returned is a copy of the original data, not a view as one gets for slices. |
| 5 | + |
| 6 | +Index arrays must be of integer type. Each value in the array indicates which value in the array |
| 7 | +to use in place of the index. Negative values are permitted and work as they do with single indices or slices. |
| 8 | + |
| 9 | +```python |
| 10 | +x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 11 | +a = np.array([0, 3, 5, 2]) |
| 12 | +print(x[a]) |
| 13 | +``` |
| 14 | +Output: |
| 15 | +```text |
| 16 | +[1 4 6 3] |
| 17 | +``` |
| 18 | + |
| 19 | +When index arrays are used, what is returned is an array with the same shape as the index |
| 20 | +array, but with the type and values of the array being indexed: |
| 21 | +```python |
| 22 | +b = np.array([[9, 9], [0, 1]]) |
| 23 | +c = x[b] |
| 24 | +print(c) |
| 25 | +``` |
| 26 | +Output: |
| 27 | +```text |
| 28 | +[[10 10] |
| 29 | + [ 1 2]] |
| 30 | +``` |
| 31 | + |
| 32 | +### Indexing multidimensional arrays |
| 33 | +Things become more complex when multidimensional arrays are indexed, particularly with multidimensional index arrays. |
| 34 | +Let's consider this: |
| 35 | + |
| 36 | +```python |
| 37 | +x = np.arange(15).reshape(5, 3) |
| 38 | +print(x) |
| 39 | +print(x[np.array([0, 1, 4])]) # 1 |
| 40 | +print(x[np.array([0, 1, 4]), np.array([0, 0, 1])]) # 2 |
| 41 | +``` |
| 42 | +Output: |
| 43 | +```text |
| 44 | +[[ 0 1 2] |
| 45 | + [ 3 4 5] |
| 46 | + [ 6 7 8] |
| 47 | + [ 9 10 11] |
| 48 | + [12 13 14]] |
| 49 | + |
| 50 | +[[ 0 1 2] |
| 51 | + [ 3 4 5] |
| 52 | + [12 13 14]] |
| 53 | + |
| 54 | +[ 0 3 13] |
| 55 | +``` |
| 56 | +You can see that in the first case, a new array is constructed where |
| 57 | +each value of the index array selects one row from the indexed array and the resulting |
| 58 | +array has the shape `(number_of_index_elements, size_of_row)`. |
| 59 | + |
| 60 | +In the second case, the resulting array has the same shape as the index arrays, and the values |
| 61 | +correspond to the index set for each position in the index arrays. In this example, the first |
| 62 | +index value is `0` for both index arrays, and thus the first value of the resulting array is `x[0, 0]`. |
| 63 | +The next value is `y[1, 0]`, and the last is `y[4, 1]`. |
| 64 | + |
| 65 | +If the index arrays do not have the same shape, there is an attempt to broadcast them to the same shape. |
| 66 | +If they cannot be broadcast to the same shape, an exception is raised. |
| 67 | + |
| 68 | +The broadcasting mechanism permits index arrays to be combined with scalars for other indices. |
| 69 | +The scalar value is used for all the corresponding values of the index arrays: |
| 70 | + |
| 71 | +```python |
| 72 | +print(x[np.array([0, 2, 3, 4]), 1]) |
| 73 | +``` |
| 74 | +Output: |
| 75 | +```text |
| 76 | +[ 1 7 10 13] |
| 77 | +``` |
| 78 | +You can read more about integer indexing [here](https://numpy.org/doc/stable/reference/arrays.indexing.html#integer-array-indexing). |
| 79 | + |
| 80 | +### Task |
| 81 | +1. Using integer array indexing create an array `a` that contains elements with |
| 82 | + indices `[7, 13, 28, 33]` from the array `x`. |
| 83 | + Then create a 2-D array `b` with shape `(3, 3)` that contains elements with indices |
| 84 | + `[0, 1, 2], [10, 11, 12], [28, 29, 30]` from the array `x`. |
| 85 | + |
| 86 | +2. Based on the 2-D array `y`: |
| 87 | + - Create an array `c` containing rows number `0`, `2` and `4`. |
| 88 | + - Create a 1-D array `d`, containing elements `0`, `1` and `2` from the rows `0`, `2` and `4`, respectively. |
| 89 | + - Create a 1-D array `e`, containing elements with index `6` from rows `1`, `2` and `4`. |
| 90 | + |
0 commit comments