Rescaling Vectors by Sum of Magnitudes vs Euclidean Length
Machine LearningModel answerLast updated
Compare dividing a vector by its L1 norm versus its L2 norm: definitions, geometry, a worked example, edge cases, and when each rescaling suits a machine learning task.
You are asked to explain, as you would in a spoken technical discussion, how rescaling a vector by its L1 norm differs from rescaling it by its L2 norm.
Your answer should:
- Define both norms and the vector each rescaling produces.
- Work through a small numeric example that makes the difference visible.
- Describe what each rescaled vector represents geometrically and what downstream uses it supports.
- Explain how each behaves when one component dominates, when values are negative, and when the vector is all zeros.
- Keep this topic clearly separate from L1/L2 regularization of model weights and from per-feature standardization (z-scoring).
Before answering, state which interpretation you are using. If the question is ambiguous, say so, and also say whether the rescaling is applied per sample (row) or per feature (column).
Hints
Hint 1
Write both formulas side by side: the L1 norm sums absolute values, the L2 norm is the square root of the sum of squares. Normalizing means dividing the vector by the chosen norm.
Hint 2
Pick a vector like (3, -4) or (1, 2, 2, 5). Compute both results and check what is guaranteed afterward: which quantity equals 1 in each case?
Hint 3
Think about what each result is used for: L1-normalized nonnegative vectors behave like distributions, while L2-normalized vectors lie on the unit sphere, where dot product equals cosine similarity.
Hint 4
Do not forget the zero vector (division by zero) and the fact that normalization of data is a preprocessing step, unlike regularization, which adds a penalty to the loss.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying the question
"Normalization" here means rescaling a vector so a chosen norm equals 1. It is not L1/L2 regularization (a penalty on model weights) and not standardization (subtract mean, divide by standard deviation per feature). I will assume the rescaling is applied per sample (row), which is the common meaning in scikit-learn's Normalizer, and I will flag the difference from column-wise scaling at the end.
Definitions
For a vector x = (x₁, …, xₙ):
- L1 norm: ‖x‖₁ = Σ |xᵢ|
- L2 norm: ‖x‖₂ = √(Σ xᵢ²)
Normalization divides by the norm:
- L1 normalization: x / ‖x‖₁, so the absolute values sum to 1.
- L2 normalization: x / ‖x‖₂, so the squares sum to 1 (Euclidean length 1).
Both preserve the direction of the vector in the sense that every component is scaled by the same positive factor. They remove magnitude and keep the relative proportions, but the two scale factors differ.
Worked example
Take x = (1, 2, 2, 5).
- ‖x‖₁ = 1 + 2 + 2 + 5 = 10, so L1-normalized: (0.1, 0.2, 0.2, 0.5). Sum = 1.
- ‖x‖₂ = √(1 + 4 + 4 + 25) = √34 ≈ 5.83, so L2-normalized: (0.171, 0.343, 0.343, 0.857). Sum of squares = 1.
Notice that the L2 version gives the large component (5) a bigger share of the result (0.857 versus 0.5), because squaring emphasizes large values. L1 keeps proportions linear.
With negatives, x = (3, -4): ‖x‖₁ = 7 giving (0.43, -0.57), and ‖x‖₂ = 5 giving (0.6, -0.8). In the L1 case the signed sum is not 1; it is the absolute values that sum to 1. Signs are preserved in both.
Geometry
- The set {x : ‖x‖₁ = 1} is a diamond (cross-polytope). In the nonnegative orthant it is the probability simplex.
- The set {x : ‖x‖₂ = 1} is the unit sphere.
So L1 normalization projects (by scaling) onto the diamond, and L2 normalization projects onto the sphere. Each is a radial projection along the ray through the origin.
When to use each
L1 normalization
- Nonnegative count or frequency data where you want proportions: term frequencies, histograms, bag-of-visual-words, mixture weights.
- Outputs can be read as a distribution, which suits KL divergence, chi-squared distance, or histogram intersection.
- Interpretable: each entry is that feature's share of the total.
L2 normalization
- Similarity and retrieval: after L2 normalization, the dot product equals cosine similarity, and Euclidean distance becomes a monotone function of it (‖a−b‖² = 2 − 2·cos). This is standard for embeddings, TF-IDF vectors, and nearest-neighbor search.
- Models sensitive to vector length, such as linear models, SVMs with dot-product kernels, and neural network inputs, where a unit-length input prevents long documents or large-magnitude samples from dominating.
- Smooth and differentiable everywhere except the origin, which helps gradient-based methods.
Behavior in edge cases
- One dominant component: L2 gives the dominant entry most of the weight and squashes the rest toward 0. L1 keeps the proportions linear, so small entries retain more relative influence. Neither is robust to outliers in the sense of resisting them; they only rescale.
- Negative values: both keep signs. If you want a distribution, take absolute values or apply softmax first, because L1 normalization of signed data does not give a probability vector.
- Zero vector: both norms are 0, so division is undefined. Libraries leave the vector as zeros (or add a small epsilon to the denominator). Handle this explicitly, since empty documents or all-zero rows occur in practice.
- Sparsity: both preserve zeros, so sparse inputs stay sparse.
Do not confuse with related ideas
- L1/L2 regularization adds λ‖w‖₁ or λ‖w‖₂² to the loss to constrain model weights. L1 encourages sparse weights; L2 shrinks them smoothly. It acts on parameters during training, not on data.
- Standardization works per feature (column), centering and scaling to unit variance. Vector normalization works per sample (row). They solve different problems and are often used together or in sequence.
- Min-max scaling maps each feature to a fixed range and is also unrelated to norms.
Code
import numpy as np
def normalize(x, p=2, eps=1e-12):
x = np.asarray(x, dtype=float)
norm = np.sum(np.abs(x)) if p == 1 else np.sqrt(np.sum(x**2))
return x / max(norm, eps) # zero vector stays zero
x = [1, 2, 2, 5]
print(normalize(x, 1)) # [0.1 0.2 0.2 0.5]
print(normalize(x, 2)) # [0.171 0.343 0.343 0.857]
Complexity is O(n) time and O(n) space per vector (O(1) extra if done in place).
Common mistakes
- Mixing up normalization with regularization.
- Claiming the L1-normalized vector sums to 1 when it has negative entries; only the absolute values do.
- Forgetting the zero vector.
- Applying row normalization when column-wise scaling is what the model needs, or vice versa.
- Using L1 normalization and then cosine similarity, which assumes unit L2 length.
Summary
Same idea, different yardstick: L1 makes the components' magnitudes sum to 1 (proportions, distributions), while L2 makes the Euclidean length 1 (unit sphere, cosine similarity). Choose by what downstream computation needs: shares of a total versus angles between directions.