Skip to content

Vector Operations: Dot Product, Norms, and Distances

Vectors are the building blocks of machine learning. Beyond representing features, we often need to measure similarity, magnitude, and distance between vectors. These concepts form the basis of algorithms like k-Nearest Neighbors, clustering, and gradient computations.

Dot Product (Inner Product)

The dot product of two vectors a,bRn is:

ab=i=1naibi

Explanation of Dot Product

  • Geometrically: measures how much two vectors point in the same direction.
  • If ab>0: they point roughly the same way.
  • If ab<0: they point in opposite directions.
  • If ab=0: they are orthogonal (perpendicular).

Mini Example:

[1,2,3][4,5,6]=14+25+36=32

Vector Norms (Magnitude / Length)

A norm measures the size or length of a vector. The most common is the Euclidean norm (L2):

a2=a12+a22++an2

Other norms:

  • L1 norm (Manhattan distance): a1=|ai|
  • L norm (Max norm): a=max|ai|

Explanation of Norms

  • a2 = straight-line length from origin.
  • a1 = “grid path” length (like walking city blocks).
  • a = maximum single coordinate size.

Mini Example:
For a=[3,4]:

  • a2=32+42=5
  • a1=3+4=7
  • a=max(3,4)=4

Distances Between Vectors

The distance between two vectors measures how far apart they are.

  • Euclidean Distance (L2):

    d(a,b)=ab2
  • Manhattan Distance (L1):

    d(a,b)=ab1
  • Cosine Similarity (based on dot product):

    cosθ=abab

Explanation of Distances

  • Euclidean = straight-line distance.
  • Manhattan = sum of coordinate-wise differences.
  • Cosine similarity = angle between vectors; used in NLP & recommendation systems.

Mini Example:
For a=[1,2], b=[4,6]:

  • Euclidean: (41)2+(62)2=25=5
  • Manhattan: |41|+|62|=3+4=7
  • Cosine similarity: 14+2612+2242+62=165520.99

Hands-on with Python and Rust

python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Dot product
dot = np.dot(a, b)

# Norms
norm2 = np.linalg.norm(a, 2)
norm1 = np.linalg.norm(a, 1)
norm_inf = np.linalg.norm(a, np.inf)

# Distances
euclidean = np.linalg.norm(a - b)
manhattan = np.sum(np.abs(a - b))
cosine = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

print("Dot product:", dot)
print("L2 norm of a:", norm2)
print("Euclidean distance:", euclidean)
print("Cosine similarity:", cosine)
rust
use ndarray::array;
use ndarray::Array1;

fn dot(a: &Array1<f64>, b: &Array1<f64>) -> f64 {
    a.dot(b)
}

fn norm_l2(a: &Array1<f64>) -> f64 {
    a.dot(a).sqrt()
}

fn norm_l1(a: &Array1<f64>) -> f64 {
    a.iter().map(|x| x.abs()).sum()
}

fn euclidean_distance(a: &Array1<f64>, b: &Array1<f64>) -> f64 {
    ((a - b).mapv(|x| x.powi(2))).sum().sqrt()
}

fn cosine_similarity(a: &Array1<f64>, b: &Array1<f64>) -> f64 {
    dot(a, b) / (norm_l2(a) * norm_l2(b))
}

fn main() {
    let a = array![1.0, 2.0, 3.0];
    let b = array![4.0, 5.0, 6.0];

    println!("Dot product: {}", dot(&a, &b));
    println!("L2 norm of a: {}", norm_l2(&a));
    println!("L1 norm of a: {}", norm_l1(&a));
    println!("Euclidean distance: {}", euclidean_distance(&a, &b));
    println!("Cosine similarity: {}", cosine_similarity(&a, &b));
}

Connection to ML

  • Dot product → similarity, projections, neural network activations.
  • Norms → regularization, measuring feature magnitudes.
  • Distances → kNN, clustering, recommender systems, embeddings.

Next Steps

Continue to Matrix Operations: Multiplication, Transpose, and Inverse.