Vector Operations - Dot Product, Norms, and Distances
Vector Operations: Dot Product, Norms, and Distances
Section titled “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)
Section titled “Dot Product (Inner Product)”The dot product of two vectors is:
::: info Explanation of Dot Product
- Geometrically: measures how much two vectors point in the same direction.
- If : they point roughly the same way.
- If : they point in opposite directions.
- If : they are orthogonal (perpendicular).
:::
Mini Example:
Vector Norms (Magnitude / Length)
Section titled “Vector Norms (Magnitude / Length)”A norm measures the size or length of a vector. The most common is the Euclidean norm ():
Other norms:
- norm (Manhattan distance):
- norm (Max norm):
::: info Explanation of Norms
- = straight-line length from origin.
- = “grid path” length (like walking city blocks).
- = maximum single coordinate size.
:::
Mini Example:
For :
Distances Between Vectors
Section titled “Distances Between Vectors”The distance between two vectors measures how far apart they are.
-
Euclidean Distance ():
-
Manhattan Distance ():
-
Cosine Similarity (based on dot product):
::: info 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 , :
- Euclidean:
- Manhattan:
- Cosine similarity:
Hands-on with Python and Rust
Section titled “Hands-on with Python and Rust”::: code-group
import numpy as np
a = np.array([1, 2, 3])b = np.array([4, 5, 6])
# Dot productdot = np.dot(a, b)
# Normsnorm2 = np.linalg.norm(a, 2)norm1 = np.linalg.norm(a, 1)norm_inf = np.linalg.norm(a, np.inf)
# Distanceseuclidean = 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)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
Section titled “Connection to ML”- Dot product → similarity, projections, neural network activations.
- Norms → regularization, measuring feature magnitudes.
- Distances → kNN, clustering, recommender systems, embeddings.
Next Steps
Section titled “Next Steps”Continue to Matrix Operations: Multiplication, Transpose, and Inverse.