Norm and similarity intuition
A vector norm measures its size. The Euclidean or L2 norm is the square root of the sum of squared entries. For [3, 4], it is √(9 + 16) = 5. Euclidean distance is the norm of the difference between two vectors. Cosine similarity instead compares their directions by dividing their dot product by the product of their norms. The two measures answer different questions.
Measure length and distance
The vector [6, 8] is twice [3, 4], so its norm is 10. The difference [6−3, 8−4] has norm 5. If keyword counts encode two topics, Euclidean distance treats a document with twice as many matches as different in magnitude. Coordinates must use comparable units or a deliberate scaling policy: a large numerical feature can dominate distance. L1 norm is the sum of absolute entries, so [3, -4] has L1 norm 7 and L2 norm 5. Neither norm is always better.
def norm(v): if not v: raise ValueError("Use a nonempty vector") return sum(x * x for x in v) ** 0.5
def distance(a, b): if len(a) != len(b) or not a: raise ValueError("Use equal, nonempty vectors") return norm([a[i] - b[i] for i in range(len(a))])
print(norm([3, 4]))print(norm([6, 8]))print(distance([3, 4], [6, 8]))The expression ** 0.5 calculates a square root; the comprehension constructs differences. The examples use small integers and return floating-point results. Extreme values can overflow or lose precision; robust numerical libraries and stable algorithms belong to the specialist ML track.
Compare direction with cosine similarity
For nonzero real vectors a and b, cosine similarity is dot(a, b) / (norm(a) × norm(b)). Its mathematical range is −1 to 1: parallel same-direction vectors give 1, perpendicular vectors give 0, and opposite directions give −1. For nonnegative feature counts, values are nonnegative. For [3, 4] and [6, 8], the numerator is 50 and the denominator is 5 × 10 = 50, so the similarity is 1. Multiplying either vector by a positive scalar does not change cosine; it does change the norm. A zero vector has no direction and makes this mathematical formula undefined.
def cosine(a, b): if len(a) != len(b) or not a: raise ValueError("Use equal, nonempty vectors") aa = sum(x * x for x in a) bb = sum(x * x for x in b) if aa == 0 or bb == 0: return None ab = sum(a[i] * b[i] for i in range(len(a))) return ab / (aa ** 0.5 * bb ** 0.5)
print(round(cosine([3, 4], [6, 8]), 6))print(round(cosine([1, 0], [0, 1]), 6))print(round(cosine([1, 0], [-1, 0]), 6))print(cosine([0, 0], [1, 0]))Here None means “undefined under this contract,” not zero similarity. A numerical library may deliberately return another value for a zero row, so check its documented convention. round is used only for readable display; compare approximate values using a declared tolerance, such as absolute error at most 0.000001 for these bounded examples. Rounding is not a general cure for numerical errors.
Choose a measure for the decision
A fictional search query has topic-count vector [1, 1]. Descriptions [2, 2] and [10, 0] have cosine scores 1 and about 0.7071 respectively. A raw dot-product rank instead favors the second description (10 versus 4). The difference arises from magnitude, not necessarily meaning. Cosine only compares the chosen representation; it does not establish relevance, causation, semantic understanding or truth. A real search system needs judged relevance and a policy for empty or unsupported queries.
Practice and recap
Compare [1, 0] with [3, 0] and [0, 1] by dot product, distance and cosine. Check: dot products 3 and 0; distances 2 and √2; cosines 1 and 0. Explain why distance ranks the second vector closer while cosine prefers the first. What should happen for [0, 0]? Under our contract return None and report an undefined comparison. Next chapter: probability, statistics and uncertainty.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Norm and similarity intuition — understanding check
Apply introductory linear algebra to small original fixtures.
What is the L2 norm of [3,4]?
What is cosine similarity for [3,4] and [6,8]?
What does the lesson cosine helper return for [0, 0] and [1, 0]?
For a nonzero vector v, which quantity changes when v is replaced by 2v?
Why can a high cosine score still rank an irrelevant document highly?