Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Vector normalization makes embedding-based retrieval more reliable by ensuring similarity depends on direction, not magnitude. In this task, you’ll normalize a batch of embedding vectors so you can safely use cosine similarity later.
Implement the function
Rules:
sklearn.preprocessing.normalize).Output:
| Argument | Type |
|---|---|
| embeddings | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Return NumPy array.
Compute row-wise L2 norms (axis=1).
No sklearn/normalization utilities allowed.
Compute a per-row L2 norm: norms = np.sqrt(np.sum(embeddings**2, axis=1, keepdims=True)).
Normalize with broadcasting: embeddings / norms. Use keepdims=True in summation to ensure shapes match.
Handle division by zero (if any norm is 0) by setting that norm to 1 or using np.maximum.