Multi-Dimensional Array Slicing (Row, Column & Matrix Slices)
Multi-Dimensional Array Slicing (Row, Column & Matrix Slices)
In multi-dimensional arrays, slicing generalizes across multiple axes. Unlike standard Python (which requires clumsy chained brackets like matrix[row][col]), NumPy allows clean comma-separated slicing along every axis simultaneously: arr[row_slice, col_slice].
1. The 2D Coordinate Syntax: arr[rows, cols]
In a 2D matrix:
- Axis 0 represents Rows (vertical dimension).
- Axis 1 represents Columns (horizontal dimension).
2. Extracting Complete Rows and Columns
By using a lone colon : for an axis, you select all elements along that dimension:
3. Sub-Matrix Slicing (2D Blocks)
You can slice rows and columns simultaneously to extract sub-grids:
4. Preserving Dimensions vs Reducing Dimensions
Notice the subtle difference between slicing with a single integer versus a slice range:
In machine learning algorithms and neural networks, keeping rank-2 shapes (1, 5) is often mandatory for matrix multiplication!
5. The Ellipsis Operator (...)
When slicing arrays with 4, 5, or more dimensions (such as video frames: [batch, frames, height, width, channels]), writing colons for every axis becomes tedious. The Ellipsis (...) automatically expands to as many full-slice colons : as needed:
Multiple Choice Questions
1. In a 2D NumPy array 'arr', how do you extract the entire third column across all rows?
A. arr[3, :] B. arr[:, 2] C. arr[2, :] D. arr[:, 3] Answer: B Explanation: Python uses 0-based indexing; column 3 is index 2. arr[:, 2] selects all rows (:) in column index 2.
2. How does 'arr[0, :]' differ from 'arr[0:1, :]'?
A. arr[0, :] returns a 1D vector of shape (N,); arr[0:1, :] preserves rank-2 dimensionality returning shape (1, N) B. They return completely different numbers C. arr[0:1, :] deletes the row D. There is no difference Answer: A Explanation: Indexing with a scalar integer reduces the dimension (rank), returning a 1D array; slicing with a range [0:1] retains the 2D matrix structure.
3. What does the Ellipsis operator '...' do in multidimensional slicing: 'tensor[..., 0]'?
A. Stops the execution B. Expands to fill as many complete slice colons (:) as necessary to span intermediate unspecified dimensions C. Fills the array with dots D. Generates random numbers Answer: B Explanation: The Ellipsis (...) represents all intervening dimensions not explicitly specified in the slice.
4. If matrix M has shape (6, 8), what will be the shape of 'M[2:5, 3:7]'?
A. (3, 4) B. (2, 3) C. (6, 8) D. (5, 7) Answer: A Explanation: Rows 2:5 yields 3 rows (indices 2, 3, 4). Columns 3:7 yields 4 columns (indices 3, 4, 5, 6). The resulting shape is (3, 4).
5. Why is 'matrix[1, 2]' preferred over 'matrix[1][2]' in NumPy?
A. matrix[1][2] creates a temporary intermediate array object for matrix[1], adding unnecessary overhead, whereas matrix[1, 2] accesses memory directly in one step B. matrix[1][2] is illegal syntax in Python C. matrix[1, 2] converts the number to string D. It saves the file to disk Answer: A Explanation: Chained indexing (matrix[1][2]) creates an unnecessary intermediate 1D slice object; multidimensional comma indexing (matrix[1, 2]) computes the memory offset directly in C.
Memory Views vs Deep Copies (view() vs copy())
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| 1D Array Indexing, Slicing & Step Strides | Memory Views vs Deep Copies (view() vs copy()) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.