Lesson 12: NumPy Basics – Arrays & Vector Operations

1. What is NumPy?

NumPy = Numerical Python — foundation for almost all ML libraries.

Provides fast arrays (ndarrays) and math operations — much faster than Python lists.

Install (if needed): pip install numpy

Import convention: import numpy as np

Exercise 1

What does this create?
import numpy as np
a = np.array([1, 2, 3])

2. Creating Arrays

# 1D array
a = np.array([1, 2, 3])

# 2D array (matrix)
b = np.array([[1, 2], [3, 4]])

# Special arrays
zeros = np.zeros(5)          # [0. 0. 0. 0. 0.]
ones = np.ones((2, 3))       # 2×3 matrix of 1s
range_arr = np.arange(0, 10, 2)  # [0 2 4 6 8]
linspace = np.linspace(0, 1, 5)  # [0.   0.25 0.5  0.75 1.  ]

Exercise 2

Create a 3×3 array of zeros:
z = np.((3, 3))
print(z.shape) →

Exercise 3

Which are advantages of NumPy arrays over Python lists?
← Previous Lesson (11) Next Lesson (13) →