A 5-minute Python & NumPy primer

This book assumes no prior experience with Python, AI, or any library. This page gives you just enough to read every code example. If you already know Python and NumPy, skip ahead.

What is Python?

Python is a programming language — a way to write step-by-step instructions for a computer. You write text in a file ending in .py and run it. Throughout this book, grey boxes contain Python code, and the box right after shows what it prints when you run it:

print("hello")
print(2 + 3)

Output:

hello
5

print(...) displays a value on the screen. That's how we'll inspect results.

Variables, functions, lists

x = 10                 # a variable: the name x now refers to the number 10
name = "video"         # text (a "string") goes in quotes
nums = [4, 1, 7]       # a list: an ordered collection of values

A function is a reusable recipe. You "call" it with inputs (called arguments) in parentheses, and it gives back a result:

def double(value):     # define a function named "double"
    return value * 2   # "return" hands a result back to the caller

print(double(21))      # call it

Output:

42

What is NumPy?

NumPy is a Python library (a bundle of pre-written code you can reuse) for working with arrays of numbers efficiently. We load it once at the top of a file and, by convention, nickname it np:

import numpy as np

Arrays: 1-D (a list of numbers) and 2-D (a grid/matrix)

import numpy as np

v = np.array([1.0, 2.0, 3.0])          # a 1-D array (a "vector")
M = np.array([[1.0, 2.0],              # a 2-D array (a "matrix" / grid)
              [3.0, 4.0]])
print(v)
print(M)
print("shape of M:", M.shape)          # (rows, columns)

Output:

[1. 2. 3.]
[[1. 2.]
 [3. 4.]]
shape of M: (2, 2)
  • A vector is just a row of numbers. In this book, each video frame is described by a vector of numbers (its "features" — think of it as a numeric fingerprint of the image).
  • A matrix is a grid of numbers (rows × columns). M.shape tells you its size as (rows, columns).

The handful of NumPy operations we use

You'll seeWhat it means
A @ BMatrix multiplication (a specific way to combine two grids of numbers).
A.TTranspose — flip a matrix over its diagonal (rows become columns).
np.sum(A)Add up all the numbers in A.
np.cumsum(a)Running totals: [1,2,3][1,3,6].
np.diag(M)The diagonal entries of a square matrix (top-left to bottom-right).
A.shapeThe size of an array, as (rows, columns).
np.zeros((r, c))A grid of the given size filled with 0.
np.arange(k)The whole numbers 0, 1, ..., k-1.

You don't need to memorize these — each is explained again the first time it appears.

One more idea: the dot product

The dot product of two vectors multiplies them position-by-position and adds up the results. It's the basic "how aligned are these two things?" measurement, and it's the seed of everything in this book:

import numpy as np
a = np.array([1.0, 0.0])
b = np.array([0.9, 0.1])
print(np.dot(a, b))     # 1*0.9 + 0*0.1

Output:

0.9

A big dot product means the two vectors point in a similar direction (the frames look alike); a small one means they're different. Hold onto that intuition — the next chapter builds the whole method on it. 👉