Hi Pythonistas!
In the last post we spun and scaled shapes with matrix math. Today’s building block is learning how computers measure size and distance in multi-dimensional space a superpower in everything from clustering phones by battery life and price to flagging outlier credit card transactions.
What Are Norms?
A norm basically answers: "How big is this vector?". Think of a vector as a set of numbers, like [3,−4,5][3,−4,5].
The norm gives a single number to represent its length or size.
Two types you'll see everywhere:
L1 Norm (Manhattan distance):
Add up the absolute values.Like counting how many blocks you’d walk in a city grid.
L1 norm of [3,−4,5]=∣3∣+∣−4∣+∣5∣=12
L1 norm of [3,−4,5]=∣3∣+∣−4∣+∣5∣=12
L2 Norm (Euclidean distance):
Classic straight-line length think Pythagoras.
L2 norm of [3,−4,5]= sqrt((3^2)+(−4^2)+(5^2))= sqrt(9+16+25)= sqrt(50)≈7.07
How Far Apart? Distance Between Vectors
If you have two vectors, their distance is:
L1 distance: Add the absolute differences.
L2 distance: Square root of the sum of squared differences (straight-line).
code
import numpy as np
a = np.array([3, -4, 5])
b = np.array([0, 0, 0])
l1_norm = np.sum(np.abs(a))
l2_norm = np.linalg.norm(a)
l1_dist = np.sum(np.abs(a - b))
l2_dist = np.linalg.norm(a - b)
print("L1 norm of a:", l1_norm)
print("L2 norm of a:", l2_norm)
print("L1 distance between a and b:", l1_dist)
print("L2 distance between a and b:", l2_dist)
Output
L1 norm of a: 12
L2 norm of a: 7.0710678118654755
L1 distance between a and b: 12
L2 distance between a and b: 7.0710678118654755
Why Does This Matter? (Practical Applications)
- Machine Learning Algorithms: k-Nearest Neighbors uses distances to "find your friends." Clustering groups similar items by closeness.
- Regularization: L1/L2 norms keep model weights in check helping avoid overfitting.
- Anomaly Detection: Flag outliers by checking distances from the majority.
- Image & Audio Processing: Compare brightness, features, or signals with norms.
- Robotics & Path Planning: Measure how far a robot (or drone) must go.
- Recommendation Engines: Find similar products by closeness in "feature space."
What I Learned
Norms and distances are the secret language for "how big?"" and "how far?" vital for any project that needs to compare, group, or flag data.
It's the math glue that holds ML, AI, and data analysis together!
What’s Next?
Next, I'll turn these distance tricks into a mini-project: building k-NN from scratch to classify things based on their nearest neighbors.