Normalize a vector to unit length. Essential before cosine similarity โ ensures dot product equals cosine similarity.
x_norm[i] = x[i] / ||x||` where `||x|| = sqrt(ฮฃ x[i]ยฒ)l2_normalize([3, 4]) โ [0.6, 0.8] # magnitude = 5 โ 3/5, 4/5
Round to **5 decimal places**.
Similar Problems
Test Cases (2 visible ยท 2 hidden)
Case 1: 3-4-5 right triangle
Input: l2_normalize([3,4])
Expected: [0.6, 0.8]
Case 2: Already unit vector
Input: l2_normalize([1,0,0])
Expected: [1.0, 0.0, 0.0]
โโต Run ยท โโงโต Submit