So far, we've treated vectors as directions and data points. We know how to add and scale them. But this leaves us with some fundamental unanswered questions:
- How long is a vector?
- What's the distance between two vectors?
- How can we measure the relationship or "agreement" between two vectors?
To answer these, we need to introduce a new set of tools for measurement. We'll start with the concept of "length," formally known as the norm.
The L2 Norm (The One You Know)
Let's take our vector . When we draw it, what's its length? You probably see the answer instantly. The vector forms the hypotenuse of a right-angled triangle with sides of length 3 and 4. We can use the Pythagorean theorem!
Length² = 3² + 4² = 9 + 16 = 25
Length = √25 = 5
This is the L2 Norm. It's the standard, "as the crow flies" Euclidean distance.
The Formula: L2 Norm
For a vector , its L2 norm, written as , is:
The L1 Norm (The "Manhattan" Distance)
What if you're not a crow? What if you're a taxi driver in Manhattan, forced to travel along a grid? The distance you'd travel for the vector is simply . This is the L1 Norm. You just sum the absolute values of the components.
The Formula: L1 Norm
For a vector , its L1 norm, written as , is:
The Data Scientist's View (The Calculation)
The dot product of two vectors, and , is found by multiplying their corresponding components and then summing the results. Let and . The dot product, written , is:
The Physicist's View (The "Projection" Intuition)
The dot product tells us about the agreement between two vectors. It answers the question: "How much is vector pointing in the same direction as vector ?"
This relationship between the dot product and the angle between vectors is formalized by this crucial equation:
The Geometric Definition of the Dot Product
Where and are the L2 norms (lengths) of the vectors, and (theta) is the angle between them.
It will always be between -1 and 1, and it's one of the most important metrics in all of data science.
- Value of 1: The vectors point in the exact same direction (angle is 0°).
- Value of 0: The vectors are orthogonal (angle is 90°).
- Value of -1: The vectors point in opposite directions (angle is 180°).
Real-World Example: Recommending Movies
Imagine a streaming service. Your taste is a vector, where each component is your rating for a movie:
- You = [5, 4, 1, ..., 5]
- Alice = [5, 5, 2, ..., 4]
- Bob = [2, 1, 5, ..., 1]
To find who is most similar to you, the service computes the cosine similarity between your vector and everyone else's. It then recommends movies that Alice loves but you haven't seen yet. This is the core principle behind many recommendation engines.
1. Norm (Length)
L2 Norm (): The standard "Euclidean" length. (Pythagorean theorem).
L1 Norm (): The "Manhattan" length. (Sum of absolute values).
2. The Dot Product ()
A simple calculation that reveals the geometric relationship between two vectors.
3. Cosine Similarity
A value from -1 to 1 that normalizes the dot product to give a pure measure of directional "agreement." The workhorse of similarity tasks.