What will this program print:
1 2 3 4 5 6 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The array shape is", array[0, :, idxs].shape) |
What will this program print:
1 2 3 4 5 6 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The array shape is", array[0, :, idxs].shape) |
The program creates a NumPy array with the shape (10, 20, 30) and then selects a slice from it using the index 0 for the first dimension, the entire slice (:) for the second dimension, and the list [1, 2, 3] for the third dimension. Step-by-step: 1. The array shape is (10, 20, 30). 2. When indexing with array[0, :, idxs]: - The first axis is fixed to index 0. - The second axis is fully sliced, so it has length 20. - The third axis uses the list [1, 2, 3] which selects 3 elements. 3. The resulting subarray has the shape (20, 3). Thus, the program will print: The array shape is (20, 3)
Testing if the previous output contains the string The array shape is (3, 20)
: False
What will this program print:
1 2 3 4 5 6 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The array shape is", array[0, :, idxs].shape) |
What will this program print:
1 2 3 4 5 6 | import numpy as np array = np.zeros((10, 20, 30)) idxs = [1, 2, 3] print("The array shape is", array[0, :, idxs].shape) |
The code prints: The array shape is (20, 3) Here's why: 1. The array is created with shape (10, 20, 30). 2. In the indexing array[0, :, idxs]: • 0 selects the first element of the first axis (reducing that dimension). • ':' selects all 20 elements from the second axis. • idxs (which is [1, 2, 3]) selects three elements along the third axis. 3. This means for each of the 20 rows (from the second axis) we pick 3 numbers (from the third axis), resulting in a shape of (20, 3). Thus, the print statement outputs: "The array shape is (20, 3)".
Testing if the previous output contains the string The array shape is (3, 20)
: False