



The process of scaling vectors to unit length (L2 normalization) or other standard forms. Normalized vectors enable cosine similarity computation via simple dot product and are essential for many embedding models and distance metrics used in vector databases.
Loading more......
On this page
Vector Normalization
Vector normalization transforms vectors to a standard scale, most commonly to unit length (magnitude = 1). This preprocessing step is fundamental to many vector search operations and embedding models.
Most common type: scale to unit length
normalized_vector = vector / ||vector||_2
where ||vector||_2 = √(Σ x_i²)
import numpy as np
vector = np.array([3, 4])
norm = np.linalg.norm(vector) # 5.0
normalized = vector / norm # [0.6, 0.8]
For normalized vectors:
cosine_similarity(a, b) = dot_product(normalized_a, normalized_b)
No need for division by magnitudes!
Use: Cosine similarity, most embedding models
vector / np.linalg.norm(vector)
Use: Probability distributions, sparse vectors
vector / np.sum(np.abs(vector))
Use: Feature scaling
(vector - vector.min()) / (vector.max() - vector.min())
Use: Statistical analysis
(vector - vector.mean()) / vector.std()
Many databases normalize automatically:
Some require explicit normalization:
Many models output normalized vectors:
Some models don't normalize:
def l2_normalize(vectors):
norms = np.linalg.norm(vectors, axis=1, keepdims=True)
return vectors / norms
import torch.nn.functional as F
normalized = F.normalize(vectors, p=2, dim=1)
import tensorflow as tf
normalized = tf.nn.l2_normalize(vectors, axis=1)
norm = np.linalg.norm(vector)
print(f"Norm: {norm}")
print(f"Is normalized: {np.isclose(norm, 1.0)}")
norms = np.linalg.norm(vectors, axis=1)
print(f"All normalized: {np.allclose(norms, 1.0)}")
axis=1 for row vectorsaxis=0 for column vectorsdef safe_normalize(vector):
norm = np.linalg.norm(vector)
if norm == 0:
return vector
return vector / norm
Negligible processing cost; enables compute savings.
Explore more items related to this one