1.1 Scalars and Vectors
: 30 minutes
This section introduces the two smallest objects in linear algebra—the scalar and the vector—and their counterparts in Python code.
Scalars
A scalar is a single (real) value, denoted by ordinary lower-cased letters (e.g., x, y, and z). We write \mathbb{R} for the space of all real-valued scalars, so that x\in\mathbb{R} says “x is a real number”.
Scalars are represented in Python by an int or a float. For example, let weight record a weight in pounds.
Python Scalars
Python offers four basic scalar types: int (whole numbers), float (decimals), bool (True/False), and str (text). Only the first two are numbers in the sense of linear algebra, though bool quietly behaves like 0 and 1 in arithmetic—a fact that becomes useful when we start counting how many rows satisfy a condition.
The function isinstance reports whether a value has a given type.
A float stores only an approximation of a decimal number, which makes exact comparison unreliable.
Because of the above, never test two floats with ==. Test instead that they are close, as in abs(a - b) < 1e-9. You will see this idiom in every graded exercise in these notes.
Vectors
In real applications it usually makes more sense to group scalars together as a vector than to let them float around as seemingly unrelated variables.
For example, in a hypothetical clinical study of BMI, the scalar measurements of one respondent—weight, height, and age—are naturally collected into a single feature vector.
A vector is a fixed-length array of scalars, called the elements (or entries, or components) of the vector. We denote vectors by bold lowercase letters, e.g., \bold{x}, \bold{y}, \bold{z}.
The standard way to visualize a vector is by vertically stacking its elements:
\bold{x} =\begin{bmatrix}x_{1} \\ \vdots \\x_{n}\end{bmatrix}. \tag{1.1}
We refer to an element of a vector using a subscript. For example, x_2 denotes the second element of \bold{x}. Since x_2 is a scalar, we do not bold it.
If a vector \bold{x} contains n elements, we use the notation \bold{x}\in\mathbb{R}^n. Here, n is called the dimension or size of the vector.
The order of the entries is part of the data. [178, 69, 46] and [69, 178, 46] are different vectors, and confusing them is how a model ends up predicting that a 46-inch-tall person weighs 69 pounds. A vector is a list of numbers plus an agreed-upon meaning for each slot.
In linear algebra we sometimes distinguish between the column vectors above and row vectors, whose elements are stacked horizontally: \bold{x} =\begin{bmatrix}x_{1}, \ldots, x_{n}\end{bmatrix}. Unless we say otherwise, “vector” in these notes means a column vector. The distinction only starts to matter in 1.4 Matrix Operations, where it decides which matrix products are legal.
Vectors in Python
In plain Python, the closest counterpart of a vector is a list.
In Python, as in most programming languages, indices start at 0 (zero-based indexing), whereas linear-algebra subscripts begin at 1 (one-based indexing). The entry that a mathematician calls x_2 is written x[1] in code.
Slicing extracts a run of consecutive entries. The slice x[a:b] includes position a and excludes position b, so x[0:2] returns the first two entries.
A list is a fine container for a vector, but it is not a vector: Python’s arithmetic operators mean something else entirely on lists. The next two exercises are about exactly that gap, which NumPy will close for us next week.