Prerequisites & Notation

Before You Begin

This chapter covers NumPy at an intermediate-to-advanced level. You should be comfortable with basic Python data structures, functions, and have a first exposure to NumPy arrays before proceeding.

  • Python data structures: lists, tuples, dicts(Review ch01)

    Self-check: Can you explain the difference between a list and a tuple?

  • Functions, closures, and decorators(Review ch02)

    Self-check: Can you write a decorator that times a function call?

  • Basic NumPy: creating arrays and element-wise operations

    Self-check: Can you create a 1-D array with np.array and compute element-wise sums?

  • Basic linear algebra: matrix dimensions, transpose

    Self-check: Do you know what shape a (3, 4) matrix has and what its transpose shape is?

  • Binary representation of floating-point numbers

    Self-check: Do you know that float64 uses 8 bytes per element?

Notation for This Chapter

Symbols and conventions used throughout this chapter.

SymbolMeaningIntroduced
β€˜npβ€˜`np`The NumPy module (import numpy as np)s01
β€˜a.shapeβ€˜`a.shape`Tuple of array dimensions, e.g., (3, 4) for a 3-by-4 matrixs01
β€˜a.stridesβ€˜`a.strides`Bytes to step in each dimension to reach the next elements01
β€˜a.dtypeβ€˜`a.dtype`Data type of array elements (e.g., float64, complex128)s01
β€˜a[i,j]β€˜`a[i, j]`Element at row i, column j (basic indexing returns a view)s02
β€˜a[mask]β€˜`a[mask]`Boolean indexing β€” selects elements where mask is True (returns a copy)s02
β€˜np.newaxisβ€˜`np.newaxis`Alias for None; inserts a length-1 axis for broadcastings03
βŠ™\odotElement-wise (Hadamard) products04
β€˜rngβ€˜`rng`A np.random.Generator instance from np.random.default_rng()s06