AI & Machine Learning

NumPy Deep Dive

Go beyond the basics. Master NumPy arrays, vectorization, broadcasting, and advanced indexing.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Go beyond the basics. Master NumPy arrays, vectorization, broadcasting, and advanced indexing. This hands-on tutorial focuses on practical implementation of numpy deep dive concepts.

NumPy Deep Dive

NumPy (Numerical Python) is the bedrock of scientific computing in Python. It provides the high-performance multidimensional array object and tools for working with these arrays.

1. Arrays vs. Lists 🏎️

Why do we use NumPy arrays instead of Python lists? Speed and Efficiency.

  • Memory: NumPy arrays use less memory.
  • Speed: NumPy operations are implemented in C, making them 50x faster.
  • Convenience: Vectorized operations make code cleaner.
import numpy as np
import time

# Speed Test
size = 1000000
list1 = range(size)
list2 = range(size)
arr1 = np.arange(size)
arr2 = np.arange(size)

# Python List
start = time.time()
result_list = [(x+y) for x,y in zip(list1, list2)]
print(f"List time: {time.time() - start:.5f} seconds")

# NumPy Array
start = time.time()
result_arr = arr1 + arr2
print(f"NumPy time: {time.time() - start:.5f} seconds")

2. Creating Arrays πŸ—οΈ

# From a list
arr = np.array([1, 2, 3])

# Zeros and Ones
zeros = np.zeros((3, 3)) # 3x3 matrix of zeros
ones = np.ones((2, 4))   # 2x4 matrix of ones

# Ranges
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
linspace_arr = np.linspace(0, 1, 5) # 5 numbers from 0 to 1

3. Dimensions, Shapes, and Reshaping πŸ“

Understanding dimensions is crucial for Deep Learning.

  • ndim: Number of dimensions (axes).
  • shape: Tuple of integers indicating the size of the array in each dimension.
  • size: Total number of elements.
arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.ndim)  # 2
print(arr.shape) # (2, 3)

# Reshaping
reshaped = arr.reshape(3, 2)
print(reshaped)
# [[1, 2],
#  [3, 4],
#  [5, 6]]

4. Broadcasting πŸ“‘

Broadcasting allows NumPy to perform operations on arrays of different shapes.

# Adding a scalar to a matrix
arr = np.array([1, 2, 3])
print(arr + 10) 
# Output: [11, 12, 13] (10 is "broadcast" to all elements)

# Adding a 1D array to a 2D array
matrix = np.ones((3, 3))
row = np.array([0, 1, 2])
print(matrix + row)
# Output:
# [[1., 2., 3.],
#  [1., 2., 3.],
#  [1., 2., 3.]]

5. Indexing and Slicing πŸ”ͺ

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

# Standard Indexing
print(arr[0, 1]) # Row 0, Col 1 -> 2

# Slicing [start:end:step]
print(arr[0:2, 1:3]) 
# Rows 0 to 1, Cols 1 to 2
# [[2, 3],
#  [5, 6]]

# Boolean Indexing (Filtering)
print(arr[arr > 5]) 
# Output: [6, 7, 8, 9]

Interactive Challenge: Image Manipulation

Images are just NumPy arrays of pixels! Let's manipulate a "dummy" image.

PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 3

What is the output of np.zeros((2,3))?

An array of 2 zeros and 3 zeros
A 2x3 matrix of zeros
A 3x2 matrix of zeros
Error

Key Takeaways

βœ… NumPy is faster and more efficient than lists.
βœ… Broadcasting simplifies math on different shapes.
βœ… Reshaping is critical for preparing data for neural networks.

What's Next?

NumPy handles the numbers. Now let's handle structured data (tables) with Pandas.

Next Chapter: Mastering Pandas DataFrames.