Vectors and dot products
A scalar is a single number. A vector is an ordered collection of numbers, such as [2, 3]. A dot product multiplies corresponding entries of two equal-length vectors and adds the products. For [2, 3] and [4, 1], the dot product is 2 × 4 + 3 × 1 = 11. It returns a scalar, not another vector. AI systems use this operation to combine features with weights and to compare representations.
Represent a sample as a vector
Imagine a fictional reading club catalog. Two features record the counts of science and history keywords in a short description. The vector [2, 3] means two science and three history matches, in that order. [3, 2] is different. A feature vector needs a shared feature definition and order, just as in Chapter 4. The vector length (dimension here) is two, regardless of the magnitude of the entries. Python lists can store these numbers, but lists themselves do not implement mathematical vector arithmetic.
Compute a weighted score by hand
Give science a weight of 4 points per match and history a weight of 1 point per match. For the sample above, science contributes 8 points and history contributes 3, so the total is 11 points. The weights are manually chosen for this example; nothing has been trained. In a linear model a similar operation can use learned weights, often with an additional intercept. The score is not a probability and does not prove that a reader will like the book.
def dot(a, b): if len(a) != len(b) or not a: raise ValueError("Use equal, nonempty vectors") total = 0 for i in range(len(a)): total += a[i] * b[i] return total
print(dot([2, 3], [4, 1]))print(dot([1, -2], [3, 4]))print(dot([0, 0], [4, 1]))The outputs are 11, -5 and 0. range(len(a)) visits valid indices from zero to length minus one. The function assumes small integer inputs and refuses unequal or empty lists before indexing. Empty vectors are excluded by this teaching contract; that is not a universal mathematical restriction. raise ValueError stops the function and reports a violated input condition. A negative result can occur when some contributions are negative.
Dot product versus elementwise multiplication
features = [2, 3]weights = [4, 1]contributions = []for i in range(len(features)): contributions.append(features[i] * weights[i])print(contributions)print(sum(contributions))The list [8, 3] is the elementwise product; 11 is its sum and therefore the dot product. Python list addition concatenates lists and list multiplication by an integer repeats elements. Neither is vector addition or general elementwise multiplication. Numeric array libraries provide different operator semantics; those are not used here.
Common mistakes and practice
Do not silently pair unequal lists using zip: ordinary zip stops at the shorter input. Also, equal length alone is not enough: [science, history] and [history, science] encode different coordinate systems. Reorder features to match the documented weights. Practice: calculate the score for [1, 5] using [4, 1], then reverse the sample without reversing weights and explain the difference. Check: 9 points versus 21. Next, use multiple vectors as rows of a matrix.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Vectors and dot products — understanding check
Apply introductory linear algebra to small original fixtures.
How is the dot product of two equal-length vectors calculated?
What is [2, 3] dot [4, 1]?
Why must a feature vector use the same coordinate order as its weights?
What should the sample dot function do for [1,2] and [3]?
Which output is the elementwise product of [2,3] and [4,1]?