Scalars and vectors
标量与向量
How one number and an ordered collection of numbers become model inputs: coordinates, operations, and meaning through a shopping example.
scalars-vectorsHow do you add two shopping lists?
Suppose a shop records only apples and loaves of bread. Represent an order with two numbers: apples first, bread second. The order means three apples and two loaves. Combining it with gives . These are invented teaching values, not collected data.
An order’s total price can be represented by a single number, a scalar. Quantities arranged in an agreed order form a coordinate vector. Order is part of meaning. If the second order stores bread before apples, adding the arrays may run without an error while producing the wrong order.
AI systems often represent an observation as a feature vector. Two coordinates might record a sensor’s readings in two frequency bands. A vector need not literally be an arrow in physical space; arrows provide a geometric picture of addition and scaling. Nor does representing data as a vector make every numerical operation meaningful for that data.
Definitions, coordinates, and linear combinations
This article uses real coordinates. Writing means that has real entries; is entry . The dimension is the number of coordinates, not a measure of the observation’s size. Mathematical indexing often starts at 1; Python lists start at 0. Row and column conventions must be kept consistent when moving into matrix calculations. Deep Learning, Section 2.1
Addition of equal-dimensional vectors and multiplication by a scalar act coordinate by coordinate:
Repeating two orders and times and combining them gives a linear combination:
Mathematics allows negative or fractional coefficients. Negative coefficients might describe a net inventory change without representing a valid customer order. Distinguish an operation being mathematically defined from its output being acceptable in an application. The zero vector has zero in every coordinate and satisfies ; also, .
Coordinates depend on the representation. The same planar displacement has different numbers when expressed along east/north axes or rotated axes. In more general vector spaces, functions and polynomials can also be vectors. Numerical coordinates are our starting point; bases and abstract vector spaces extend the picture. MML, Section 2.4
An operation you can inspect
This complete Python example calculates a linear combination and rejects unequal dimensions. The length check matters: ordinary zip stops at the shorter input and could silently drop coordinates.
def combine(a, x, b, y):
if len(x) != len(y):
raise ValueError("coordinate counts differ")
return [a * xi + b * yi for xi, yi in zip(x, y)]
x, y = [3, 2], [1, 4]
assert combine(1, x, 1, y) == [4, 6]
assert combine(2, x, -1, y) == [5, 0]
assert combine(0, x, 0, y) == [0, 0]
try:
combine(1, [3, 2], 1, [4])
raise AssertionError("dimension mismatch was accepted")
except ValueError:
pass
Processing coordinates takes time and allocates output space. This Python loop exposes the calculation and its boundaries; it is not presented as an implementation for large training jobs. Array libraries can perform the same mathematical operations through vectorized code.
Numbers can have incompatible meanings
Averaging city identifiers 3 and 5 to obtain 4 does not locate a point between the cities. Integers used as category codes are often just names. Postal codes and product IDs do not automatically acquire continuous distances because they are stored numerically.
Units affect subsequent calculations. If one coordinate uses meters and another millimeters, distances based directly on coordinate differences can give the latter disproportionate weight. Choose scaling for the task and estimate preprocessing statistics from training data only. Missing values also require a policy: replacing “unknown” with zero treats it as a measured zero.
A numerical vector is a representation, not the real object. Different objects can map to the same vector, and individual coordinates need not correspond to human-readable properties. Before assigning a psychological or semantic interpretation to a direction in an embedding space, test its relationship to the task.
Predict, then execute
- Add twice and subtract . Answer: . This can represent a net quantity; business rules determine whether subtracting an order is allowed.
- Can two three-dimensional vectors be added? Answer: yes within the same real coordinate space. Different coordinate orders or units require conversion first.
- Add a restriction allowing only nonnegative integer orders. Approach: validate types and ranges at the order-input boundary. Do not confuse this business rule with the mathematical definition of every vector.
- If you swap the apple and bread columns and then swap them back, is information lost? Answer: no if the permutation is known. Losing both column names and the permutation makes reliable interpretation impossible.
From coordinates to models
Matrix multiplication packages multiple linear combinations. Inner products combine coordinates using weights, and norms define lengths. Keep the habits established here: state what coordinates mean, their units, and the vector dimension before writing formulas. They expose many data errors that produce no runtime exception when you later work with attention, embeddings, and neural networks.
Make a prediction, then check
Two feature lists have equal length, but one starts with price and the other with weight. Can they be added directly?
Learning objectives
- Compute linear combinations and check coordinate order and dimension.
- Identify how categorical codes, units, and missing values constrain a numerical representation.
Prerequisites & connections
This article can be used as an entry point.
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Deep Learning — Chapter 2: Linear Algebra
Ian Goodfellow; Yoshua Bengio; Aaron Courville · 2016
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Recomputed order addition and 2x−y; checked rejection of mismatched dimensions.
- Distinguished numerical coordinates, abstract vectors, categorical IDs, and units; dimension is not treated as length.
- Compared the main arguments, numerical examples, exercises, and answers across languages; structural checks additionally compare display equations, source lists, and code.
- Reviewed the answer-label rendering fix: only Markdown emphasis markers changed to strong tags; normalized text and all code are identical.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §2.4 (2024-01-15 draft)
- Deep Learning — Chapter 2: Linear Algebra · §2.1
Related topics