Consider this program
1 2 3 4 5 6 7 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The first array shape is", array[0][:, idxs].shape) print("The second array shape is", array[0, :, idxs].shape) |
Consider this program
1 2 3 4 5 6 7 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The first array shape is", array[0][:, idxs].shape) print("The second array shape is", array[0, :, idxs].shape) |
The program will print: The first array shape is (20, 3) The second array shape is (3, 20) Here's why they differ: 1. The first statement, array[0][:, idxs]: • First, array[0] is executed. Since the original array has shape (10, 20, 30), array[0] returns a new array of shape (20, 30). • Then, on the (20, 30) result, the operation [:, idxs] is performed. Here, the colon (:) selects all 20 rows, and idxs (which is [1, 2, 3]) selects three columns. Since this is pure basic (slicing) indexing on a 2D array, the resulting shape is (20, 3). 2. The second statement, array[0, :, idxs]: • Here all indices are applied at once in a single indexing expression. The first index 0 selects the first element along axis 0. The colon (:) selects all 20 elements of the next axis. Finally, the list idxs (i.e. [1, 2, 3]) is an advanced index on the last axis. • When you mix advanced indexing (the list) with basic slicing (the colon) in a single operation, NumPy “lifts out” the advanced part and places its resulting dimensions at the front. This means that the advanced indexing part (of shape (3,)) comes first, followed by the shape from the basic slice (of shape (20,)). The result is an array of shape (3, 20). Thus, although both ways might at first seem to be picking the same “set” of numbers from the array, the way NumPy handles multi-step (chained) basic indexing versus a single combined indexing with a mix of basic and advanced indices leads to a difference in the ordering of the axes.
Testing if the previous output contains the string dvanced indexing
: True
Testing if the previous output contains the string (20, 3)
: True
Testing if the previous output contains the string (3, 20)
: True
Consider this program
1 2 3 4 5 6 7 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The first array shape is", array[0][:, idxs].shape) print("The second array shape is", array[0, :, idxs].shape) |
Consider this program
1 2 3 4 5 6 7 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The first array shape is", array[0][:, idxs].shape) print("The second array shape is", array[0, :, idxs].shape) |
The program will print: The first array shape is (20, 3) The second array shape is (3, 20) Here’s why they differ: 1. In the first print statement, we have: a = array[0] b = a[:, idxs] Since array[0] returns a 2D array (shape (20, 30)), then b = a[:, idxs] is evaluated on that 2D array. – The indexing a[:, idxs] uses a basic slice (:) for the first (row) dimension and an “advanced index” (the list idxs) for the second (column) dimension. – When you index a 2D array in this way, the advanced index remains in the “column” position. Therefore, you get an array of shape (20, 3) where 20 comes from the slice on the first dimension and 3 is the length of idxs. 2. In the second print statement, we have: c = array[0, :, idxs] Here all the indexing is done in one go on the original 3D array (of shape (10, 20, 30)). – The indexing expression (0, :, idxs) mixes basic indices (0 and :) with an advanced index (idxs). – Numpy’s advanced indexing rules say that if you mix advanced indexing with basic slicing, then the advanced indices are “moved to the front” in the resulting array. – In this case, idxs (with length 3) is the only advanced index, so it ends up as the first dimension of the result, while the basic slicing (:) on the second axis remains, giving a shape of (3, 20). Thus, though both expressions ultimately select the same data elements, the order of axes in the result is different because of when the advanced indexing is applied.
Testing if the previous output contains the string dvanced indexing
: True
Testing if the previous output contains the string (20, 3)
: True
Testing if the previous output contains the string (3, 20)
: True