import numpy as np

Slicing 2-D NumPy ArraysΒΆ

Slicing a 2D array uses comma-separated indices: arr[row_slice, col_slice]. Each slice follows the standard start:stop pattern. For example, np1[1, 2] gets the element at row 1, column 2. np1[0:1, 1:3] extracts a sub-matrix from row 0, columns 1-2. np1[0:2, 1:3] spans both rows to get a 2x2 block. This sub-matrix extraction is how you select feature subsets from data matrices, crop regions from images, and extract mini-batches for training neural networks.

# Slicing 2-d Numpy Arrays
np1 = np.array([
	[1,2,3,4,5],
	[6,7,8,9,10]
	])
# Return 8 
print(np1[1,2])
# Return slice 2,3
print(np1[0:1, 1:3])
# Return from both dimensions 2,3 and 7,8
print(np1[0:2, 1:3])