Representing vectors and performing basic operations like addition and scalar multiplication is common practice. But often, there is a need to measure the "size" or "length" of a vector. Consider a simple vector like . Geometrically, this represents an arrow starting at the origin (0,0) and ending at the point (3,4) in a 2D plane. How long is this arrow? The Pythagorean theorem can be used: . This intuitive idea of length is formalized using the concept of a norm.
A norm is a function that assigns a strictly positive length or magnitude to each vector in a vector space, with the exception of the zero vector, which has a length of zero. There isn't just one way to measure length. We'll look at two of the most common norms used in machine learning: the norm and the norm.
The most common way to measure vector length is the norm, also known as the Euclidean norm. This corresponds exactly to our intuitive understanding of distance in Euclidean space (the straight-line distance).
For a vector with elements, , the norm is calculated as the square root of the sum of the squared vector elements:
Notice how for our 2D example , this formula gives , matching the Pythagorean theorem.
In NumPy, you can calculate the norm using the np.linalg.norm() function. By default, this function calculates the norm.
import numpy as np
# Define a vector
v = np.array([3, 4])
# Calculate the L2 norm
l2_norm = np.linalg.norm(v)
print(f"Vector: {v}")
print(f"L2 Norm (Euclidean Length): {l2_norm}")
# Another example (3D vector)
w = np.array([1, 2, -2])
l2_norm_w = np.linalg.norm(w)
# Calculation: sqrt(1^2 + 2^2 + (-2)^2) = sqrt(1 + 4 + 4) = sqrt(9) = 3
print(f"\nVector: {w}")
print(f"L2 Norm: {l2_norm_w}")
The output will be:
Vector: [3 4]
L2 Norm (Euclidean Length): 5.0
Vector: [ 1 2 -2]
L2 Norm: 3.0
The norm is widely used in machine learning, for instance, in regularization techniques (like Ridge regression) to penalize large coefficient values and in calculating distance metrics.
Another useful way to measure the size of a vector is the norm, sometimes called the Manhattan norm or Taxicab norm. Instead of squaring the elements, the norm sums the absolute values of the elements.
For the same vector , the norm is calculated as:
Why "Manhattan norm"? Imagine you are in a city like Manhattan where streets form a grid. To get from point A to point B, you can't travel in a straight line (like the norm measures). You have to travel along the streets (horizontally and vertically). The norm represents this total distance traveled along the grid axes.
For our example vector , the norm is .
To calculate the norm in NumPy, you use the same np.linalg.norm() function but specify the order parameter ord=1.
import numpy as np
# Define a vector
v = np.array([3, 4])
# Calculate the L1 norm
l1_norm = np.linalg.norm(v, ord=1)
print(f"Vector: {v}")
print(f"L1 Norm (Manhattan Length): {l1_norm}")
# Another example (with negative values)
w = np.array([1, -2, -2])
l1_norm_w = np.linalg.norm(w, ord=1)
# Calculation: |1| + |-2| + |-2| = 1 + 2 + 2 = 5
print(f"\nVector: {w}")
print(f"L1 Norm: {l1_norm_w}")
The output will be:
Vector: [3 4]
L1 Norm (Manhattan Length): 7.0
Vector: [ 1 -2 -2]
L1 Norm: 5.0
The norm is also significant in machine learning. It tends to produce sparse results (meaning many values are zero) when used in optimization contexts, such as in Lasso regression for feature selection.
The difference between the and norms is easiest to see geometrically in 2D. The norm is the direct "as the crow flies" distance, while the norm is the distance traveled along the grid lines.
Visualization comparing the L1 (Manhattan, dashed orange) and L2 (Euclidean, solid blue) paths for the vector [3, 4] from the origin (0,0). The L2 norm represents the direct distance (5.0), while the L1 norm represents the distance traveled along the axes (3 + 4 = 7.0).
Understanding norms gives us a fundamental tool for measuring the magnitude of vectors, a concept that appears frequently when working with data and machine learning algorithms. While and are the most common, other norms exist (like the norm, a generalization, or the norm, the maximum absolute value component), but and provide a solid foundation.
Was this section helpful?
numpy.linalg.norm function, demonstrating its usage for calculating L1 and L2 norms in Python.© 2026 ApX Machine LearningEngineered with