Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Identity and diagonal matrices show up everywhere in ML/AI pipelines, from feature scaling to simple linear layers. In this task, you’ll apply a diagonal linear transform to a vector using efficient NumPy operations.
Implement the function
Rules:
Output:
| Argument | Type |
|---|---|
| x | np.ndarray |
| diag | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Do not build an n×n diagonal matrix.
Use only NumPy.
Return a NumPy array.
A diagonal matrix multiply is equivalent to element-wise multiplication of the diagonal component vector and the input vector.
Use the * operator or np.multiply() in NumPy to perform element-wise multiplication.
Avoid constructing the full diagonal matrix to save memory and computation time.