Search Knowledge

© 2026 LIBREUNI PROJECT

Advanced NumPy: Indexing, Slicing, and Reshaping

Multi-Dimensional Indexing

In the previous module, we touched on basic slicing. However, NumPy’s true power comes from its ability to manipulate multi-dimensional arrays with surgical precision.

Comma-Separated Indexing

Unlike Python lists, where you access nested elements using list[i][j], NumPy allows you to use a single set of brackets with comma-separated indices: arr[i, j]. This is not just syntactic sugar; it is more efficient and allows for complex expressions.

python
1 
2import numpy as np
3 
4arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
5 
6print("Element at [1, 2]:", arr[1, 2]) # Row 1, Col 2
7print("All rows, first column:", arr[:, 0])
8print("First two rows, last two columns:\n", arr[:2, 1:])
9 

Integer Array Indexing (Fancy Indexing)

Fancy indexing is a term for passing arrays of indices to access multiple array elements at once.

python
1 
2import numpy as np
3 
4a = np.arange(12).reshape((3, 4))
5print("Array:\n", a)
6 
7# Select elements at [0,0], [1,1], [2,0]
8rows = np.array([0, 1, 2])
9cols = np.array([0, 1, 0])
10print("\nSelected elements:", a[rows, cols])
11 

Boolean Indexing (Masking)

This is perhaps the most useful feature for data processing. You can index an array using another array of booleans of the same shape.

python
1 
2import numpy as np
3 
4arr = np.array([1, 2, 3, 4, 5, 6])
5 
6# Create a mask where elements are greater than 3
7mask = arr > 3
8print("Mask:", mask)
9 
10# Apply the mask
11print("Filtered Array:", arr[mask])
12 
13# Complex conditions
14mask_complex = (arr > 2) & (arr < 6)
15print("Filtered (2 < x < 6):", arr[mask_complex])
16 

Shape Manipulation

Frequently, data arrives in a shape that doesn’t match the input requirements of your algorithm (e.g., deep learning models often require a batch dimension).

Reshape

reshape() gives a new shape to an array without changing its data.

python
1 
2import numpy as np
3 
4# 1D array of 0-11
5a = np.arange(12)
6print("1D array:", a)
7 
8# Reshape to 3x4
9a_2d = a.reshape((3, 4))
10print("\n3x4 array:\n", a_2d)
11 
12# Flatten back to 1D
13print("\nFlattened:", a_2d.flatten())
14 

Stacking and Splitting

Combining multiple arrays into one or splitting one into many.

python
1 
2import numpy as np
3 
4x = np.array([1, 2, 3])
5y = np.array([4, 5, 6])
6 
7# Vertical Stack
8print("V-Stack:\n", np.vstack((x, y)))
9 
10# Horizontal Stack
11print("H-Stack:", np.hstack((x, y)))
12 

Broadcasting: A Preview

If you try to add a scalar to an array, NumPy implicitly expands the scalar to match the array’s shape. This is called broadcasting.

python
1 
2import numpy as np
3 
4arr = np.array([[1, 2, 3], [4, 5, 6]])
5print("Array + 10:\n", arr + 10)
6 

In the next module, we will explore the formal rules of broadcasting and how it allows for high-performance operations without unnecessary memory replication.