Dot Product
The dot product combines two vectors into one number.
It multiplies matching entries, then adds the results.
That makes it the simplest weighted-sum operation in this chapter. One vector can hold values. The other vector can hold weights. The dot product says how strongly the values match those weights.
For two vectors:
a = [2, 4]
b = [3, 5]
the dot product is:
Read it aloud:
multiply matching entries, then add
Formula
For two vectors with coordinates:
This is a sum of matching products.
The dot product needs matching shapes. If has three coordinates and has two, there is no complete set of matching pairs.
Given a = [2, 4] and b = [3, 5], compute the dot product before writing
code.
Compute it first, then check your number.
HintMultiply before adding
Pair the matching entries: 2 with 3, and 4 with 5.
SolutionTwo products, then one sum
The matching products are and . Add them:
Why It Returns One Number
Vector addition returns a vector. The dot product returns a scalar.
That scalar often acts like a score.
For example, a simple model might compute:
where stores weights and stores input features. A large positive score means the input lines up with the weights.
As a toy instance, suppose:
x = [hours_studied, hours_slept]
w = [2, 1]
Then:
w \cdot x = 2 \times \text{hours_studied} + 1 \times \text{hours_slept}The first feature counts twice as much as the second feature in this toy score. This is not yet a trained model, but it is the same shape of computation used inside linear layers.
Enter 1 if the dot product of two vectors returns one number rather than
another vector.
Compute it first, then check your number.
HintLook at the operation
Multiplying matching entries creates products, and adding them collapses the result to one number.
SolutionScalar result
Enter 1. The dot product multiplies matching entries and then adds those
products, producing one scalar.
A toy score uses and . What is ?
Compute it first, then check your number.
HintKeep the negative sign
Compute .
SolutionWeighted score
Read the first weight as adding three copies of the first feature and the
second weight as subtracting one copy of the second feature. The positive
part contributes 6; the negative part removes 5; the final scalar score
is 1.
Sign and Alignment
The dot product also carries geometric meaning.
- A positive dot product suggests the vectors point in broadly similar directions.
- A zero dot product means the vectors are perpendicular in the geometric reading.
- A negative dot product suggests the vectors point in opposing directions.
We will make this more precise when we study angles and cosine similarity. For now, keep the rough idea: dot products can behave like alignment scores.
Compute the dot product of and .
Compute it first, then check your number.
HintUse matching products
Compute .
SolutionZero dot product
Multiply matching coordinates:
The first vector is purely horizontal. The second vector is purely vertical. Neither vector has any component in the other's direction, so the dot product is zero.
Code Mirror
In NumPy, the code mirrors the formula:
import numpy as np
a = np.array([2, 4])
b = np.array([3, 5])
score = np.dot(a, b)
print(score)
The output is:
26
Let and . What is ?
Compute it first, then check your number.
HintWeighted sum
Compute .
SolutionScore computation
Keep the sign attached to the second weight:
The first feature contributes 6. The second feature is penalized by the
negative weight, so it subtracts 4. The dot product collapses those
contributions into one scalar score.
The dot product is not only arithmetic. It is also the start of measuring how two vectors point together. Next, we use that idea to define length and distance.