Back to blog
AI Systemsbeginner

NumPy Detailed Tutorial: Beginner to Advanced with Real Examples

Master NumPy step by step: ndarrays, indexing, broadcasting, vectorization, linear algebra, random sampling, performance, and practical exercises.

Asma HafeezMay 6, 20263 min read
NumPyPythonData ScienceArraysBroadcastingLinear AlgebraVectorization
Share:𝕏

NumPy Detailed Tutorial (Beginner to Advanced)

NumPy is the foundation of Python data and AI workflows. If you understand NumPy deeply, Pandas, Matplotlib, and ML libraries become much easier.


1) Install and Setup

Bash
pip install numpy
Python
import numpy as np

2) Core Concept: ndarray

Python
a = np.array([10, 20, 30, 40])
print(a.shape)   # (4,)
print(a.dtype)   # usually int64

Important attributes:

  • shape: dimensions
  • ndim: number of dimensions
  • dtype: data type
  • size: total elements

3) Creating Arrays

Python
np.zeros((2, 3))
np.ones((3, 3))
np.arange(0, 10, 2)
np.linspace(0, 1, 5)
np.eye(3)

Use linspace when you need exact number of points, not step size.


4) Indexing, Slicing, and Filtering

Python
x = np.array([5, 8, 12, 3, 9, 15])
print(x[2])       # 12
print(x[1:4])     # [ 8 12  3]
print(x[x > 8])   # [12  9 15]

2D example:

Python
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m[0, 2])    # 3
print(m[:, 1])    # column 1 -> [2 5]

5) Broadcasting (Most Important Intermediate Skill)

Broadcasting allows arithmetic between arrays of compatible shapes.

Python
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
print(a + b)

Output:

TEXT
[[11 22 33]
 [14 25 36]]

Rule summary:

  • Compare shapes from right to left
  • Dimensions are compatible if equal, or one of them is 1

6) Vectorization (Avoid Python Loops)

Python
# slow style (loop)
arr = np.arange(1_000_000)
out = [x * 2 for x in arr]

# fast NumPy style
fast = arr * 2

Vectorized operations are typically much faster and cleaner.


7) Aggregations and Statistics

Python
data = np.array([3, 5, 8, 10, 12])
print(data.sum())
print(data.mean())
print(data.std())
print(data.min(), data.max())

Axis-based example:

Python
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(mat.sum(axis=0))  # column sums
print(mat.sum(axis=1))  # row sums

8) Reshape, Stack, and Split

Python
a = np.arange(12)
b = a.reshape(3, 4)
c = np.vstack([b, b])
d = np.hstack([b, b])

Be careful: reshape requires same total element count.


9) Linear Algebra Basics

Python
A = np.array([[2, 1], [1, 3]])
B = np.array([[1, 0], [0, 1]])

print(A @ B)                 # matrix multiplication
print(np.linalg.det(A))      # determinant
print(np.linalg.inv(A))      # inverse
print(np.linalg.eigvals(A))  # eigenvalues

10) Random Sampling for Simulation

Python
rng = np.random.default_rng(42)
print(rng.integers(0, 10, size=5))
print(rng.normal(loc=0, scale=1, size=1000))

Use default_rng instead of old global random APIs for cleaner reproducibility.


11) Practical Mini Project

Build a simple sales analytics array pipeline:

  1. Generate random daily sales for 3 products over 30 days
  2. Compute total and average sales by product
  3. Find top 5 highest sales days overall
  4. Normalize each product series with z-score

12) Common Mistakes

  • Mixing Python lists and arrays unintentionally
  • Forgetting axis in reductions
  • Confusing element-wise * with matrix multiplication @
  • Using loops where vectorization is possible

Next Step

After NumPy, move to:

Enjoyed this article?

Explore the AI Systems learning path for more.

Found this helpful?

Share:𝕏

Leave a comment

Have a question, correction, or just found this helpful? Leave a note below.