Just as you can slice one-dimensional arrays to extract sequences of elements, you can also slice multi-dimensional arrays to select specific rows, columns, or rectangular blocks of data. This capability is fundamental for working with matrix-like data, such as images or tables, represented in NumPy.
For two-dimensional arrays (matrices), slicing extends the familiar start:stop:step
notation by using a comma to separate the slice specifications for each dimension. The general syntax is:
array[row_slice, column_slice]
Here, row_slice
specifies the range of rows to select, and column_slice
specifies the range of columns. Each slice follows the same rules as 1D slicing: the start
index is inclusive, the stop
index is exclusive, and the step
defines the interval. If any part of the slice (start
, stop
, or step
) is omitted, NumPy uses the defaults (start of the dimension, end of the dimension, step of 1).
Let's work with a sample 2D array to see this in action.
import numpy as np
# Create a 4x5 array (4 rows, 5 columns)
arr2d = np.array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
print("Original 2D Array:\n", arr2d)
To select a range of rows while keeping all columns, you specify the slice for the rows and use :
for the columns.
# Select the first two rows (index 0 and 1)
first_two_rows = arr2d[0:2, :]
print("\nFirst two rows:\n", first_two_rows)
# Select rows starting from index 1 up to (but not including) index 3
rows_1_and_2 = arr2d[1:3, :]
print("\nRows at index 1 and 2:\n", rows_1_and_2)
The colon :
acts as a wildcard for the column dimension, indicating that all columns should be included for the selected rows. You can also omit the colon after the comma if you are selecting full rows: arr2d[0:2]
is equivalent to arr2d[0:2, :]
.
Similarly, to select specific columns while keeping all rows, you use :
for the row slice and specify the desired column range.
# Select the first three columns (index 0, 1, 2)
first_three_cols = arr2d[:, 0:3]
print("\nFirst three columns:\n", first_three_cols)
# Select columns from index 2 up to (but not including) index 4
cols_2_and_3 = arr2d[:, 2:4]
print("\nColumns at index 2 and 3:\n", cols_2_and_3)
The real power comes when you combine row and column slicing to select a specific rectangular sub-region of the array.
# Select the sub-array from rows 0, 1 and columns 1, 2
sub_array_1 = arr2d[0:2, 1:3]
print("\nSub-array (Rows 0-1, Cols 1-2):\n", sub_array_1)
# Result: [[2, 3], [7, 8]]
# Select the sub-array from rows 1, 2 and columns 2, 3, 4
sub_array_2 = arr2d[1:3, 2:5] # or arr2d[1:3, 2:]
print("\nSub-array (Rows 1-2, Cols 2-4):\n", sub_array_2)
# Result: [[8, 9, 10], [13, 14, 15]]
The following diagram illustrates slicing the region defined by arr2d[1:3, 2:4]
, which corresponds to rows with index 1 and 2, and columns with index 2 and 3.
Visual representation of slicing
arr2d[1:3, 2:4]
. The blue highlighted elements form the resulting 2x2 sub-array containing[[8, 9], [13, 14]]
.
An important aspect to remember, consistent with 1D array slicing, is that 2D array slices are views onto the original array data, not copies. This means if you modify the elements in a slice, the changes will be reflected in the original array.
# Get a slice (view)
slice_view = arr2d[0:2, 0:2]
print("\nOriginal slice view:\n", slice_view)
# Modify an element within the slice
slice_view[0, 0] = 99
print("\nModified slice view:\n", slice_view)
# Check the original array
print("\nOriginal array after modifying slice:\n", arr2d)
# Notice the top-left element (index 0,0) is now 99
If you need a distinct copy of the slice, independent of the original array, you must explicitly use the .copy()
method.
# Create a copy of a slice
slice_copy = arr2d[2:, 2:].copy()
print("\nSlice copy:\n", slice_copy)
# Modify the copy
slice_copy[0, 0] = -1
print("\nModified slice copy:\n", slice_copy)
# Check the original array again - it remains unchanged in this region
print("\nOriginal array after modifying copy:\n", arr2d)
Mastering 2D slicing is essential for manipulating tabular data, image patches, or any grid-like structure efficiently within NumPy. It allows you to isolate and work with specific parts of your data without needing to iterate through elements manually.
© 2025 ApX Machine Learning