Slicing in multi-dimensional arrays allows us to access specific rows, columns or sub-arrays without modifying the original data. NumPy provides a simple and efficient slicing syntax that makes working with large datasets easier and more flexible.
Slicing Rows and Columns
Rows and columns are the basic components of a multi-dimensional array. NumPy allows to extract them easily using slicing, making it simple to access specific parts of the data.
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
r1 = matrix[0, :]
print(r1)
c2 = matrix[:, 1]
print(c2)
Output
[1 2 3] [2 5 8]
Explanation:
- matrix[0, :] selects the first row (0) and all columns (:).
- matrix[:, 1] selects all rows (:) and the second column (1).
- colon (:) acts as a wildcard and means "select everything along this axis."
Slicing a Subarray
A subarray is a smaller portion extracted from a larger array. This technique is useful when working with specific regions of data instead of processing the entire array.
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
res = matrix[0:2, 1:3]
print(res)
Output
[[2 3] [5 6]]
Explanation:
- 0:2 selects rows at index 0 and 1.
- 1:3 selects columns at index 1 and 2.
- result is a 2 × 2 subarray containing values from the selected rows and columns.
Slicing with Step Values
In addition to start and end positions, slicing supports a step value. A step allows to skip elements at regular intervals while traversing an array.
import numpy as np
matrix = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
res = matrix[::2, ::2]
print(res)
Output
[[ 1 3] [ 9 11]]
Explanation:
- ::2 for rows selects every second row starting from index 0.
- ::2 for columns selects every second column.
- Rows with indices 0 and 2 are selected.
- Columns with indices 0 and 2 are selected.
Negative Indexing in 2-D Arrays
Negative indexing allows to access elements starting from the end of an array instead of the beginning. This is helpful when the exact size of the array is unknown.
matrix = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]
a = matrix[-1]
print("Last Row:", a)
b = matrix[0][-1]
print("Last Element of First Row:", b)
c = matrix[1][-2:]
print("Last Two Elements of Second Row:", c)
Output
Last Row: [7, 8, 9] Last Element of First Row: 3 Last Two Elements of Second Row: [5, 6]
Explanation:
- -1 refers to the last element in a sequence.
- matrix[-1] returns the last row of the matrix.
- matrix[0][-1] accesses the last element of the first row.
- matrix[1][-2:] retrieves the last two elements from the second row.
Slicing Across Multiple Axes
In multi-dimensional arrays, slicing can be applied simultaneously across rows and columns. This allows you to extract a specific rectangular section of data in a single operation.
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
res = matrix[1:3, 0:2]
print(res)
Output
[[4 5] [7 8]]
Explanation:
- 1:3 selects rows with indices 1 and 2.
- 0:2 selects columns with indices 0 and 1.
- Both row and column slicing are performed together.
Negative Indexing in 3-D Arrays
Negative indexing works the same way in higher-dimensional arrays. It allows to access elements, rows, or columns relative to the end of each dimension.
import numpy as np
arr = np.array([ [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
[[19, 20, 21], [22, 23, 24], [25, 26, 27]] ])
res = arr[:, :, -1]
print(res)
Output
[[ 3 6 9] [12 15 18] [21 24 27]]
Explanation:
- first : selects all 2-D matrices.
- second : selects all rows within each matrix.
- -1 selects the last column from each row.
- This produces a new 2-D array containing the last column values from every matrix.
Modifying Values Using Slicing
Slices can be used not only for reading data but also for updating multiple values at once.
import numpy as np
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
matrix[0:2, 0:2] = 0
print(matrix)
Output
[[0 0 3] [0 0 6] [7 8 9]]
Explanation:
- 0:2, 0:2 selects the top-left 2 × 2 section.
- All selected values are replaced with 0.
- Slicing allows bulk updates without loops.