2/12
Arrays vs Lists: Why NumPy? · Page 2 of 2

Indexing, Slicing & Shapes

N-Dimensional Indexing

NumPy shines with multi-dimensional data (matrices, images, tensors).

2D Array Indexing

matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

matrix[0, 1]     # → 2 (Row 0, Col 1)
matrix[0, :]     # → [1, 2, 3] (Whole first row)
matrix[:, 1]     # → [2, 5, 8] (Whole second column)

Boolean Masking (Crucial for Data Science)

Instead of looping to filter, use boolean arrays:

data = np.array([10, 20, 30, 40, 50])
mask = data > 25
filtered = data[mask]  # → [30, 40, 50]

Reshaping

Neural networks and ML models require specific input shapes:

arr = np.arange(12)
arr.reshape(3, 4)  # Convert 1D to 3x4 2D
arr.reshape(-1, 1) # Automatically figure out rows, force 1 column
main.py
Loading...
OUTPUT
Click "Run Code" to execute…