Numpy Fancy Indexing
In earlier section, we discussed indexing
(arr[2])
and slicing
(arr[:5])
to fetch a single element and subset of array, respectively.
In Fancy Indexing, we pass array of indices instead of single scalar(numbers) to fetch elements at different index points. Remember that the shape of the output depends on the shape of the index arrays rather than the shape of the array being indexed
1. FANCY INDEXING 101
Let’s go through some examples to understand this concept
1.1. Case A
a. 1D Array
For 1D array, let’s suppose we want to access elements at index position of 0
, 4
and -1
b. 2D Array
For 2D array, we need to provide the index position at both axis levels. Here is an example, where we are giving the index position at both row and column. We wan to get first row, first item; second row, second item; and third row, third item:
As you can see in above example that the shape of output depends on the shape of index array and not the shape of array being indexed, which is 2D array in this example
1.2. Case B
We will go a step forward and discuss the cases where we need to fetch multiple items from each row, which in return gives us a 2D array in the output
2. COMBINED INDEXING
In this section, we will combine simple indexing technique with fancy indexing.
2.1. Case A
**Simple & Fancy indexing: **Let’s fetch 0
, 3
and -1
indexed items in the column, from the first row 0
of arr2
2.2. Case B
Fancy Indexing & Slicing: Let’s fetch 0
, 3
and -1
indexed item in the column, from the first 0
and second 1
row of arr2
Let’s fetch 0
, 3
and -1
indexed item in the column, from every other row of arr2
2.3. Case C
**Boolean Masking and Slicing: **If you haven’t covered boolean masking then you can just skip this section and come back at it again, later.
Let’s suppose, in every other row, we want to fetch all the column elements whose index position is True
(1
) in the provided array.
3. MODIFYING VALUES
In this section, we will use the fancy indexing techniques to replace the values in the array.
Let suppose, for our 1D array arr1
, we want to modify the 0
, 4
and -1
values to 0
In addition to setting the value to single number (0
in above example), we can also use operations like, +=
, -=
etc
Using the same methods, we can also modify the values in 2D array. Let modify the first and last column values of all rows to 0
Last updated
Was this helpful?