Project onto Principal Components
~15 mincode completion
Implement pca_project(X_centered, components) where components has shape (r, d).
Examples
Project onto 1 component: shape (n, 1)
- Input
- pca_project([[-2, 0], [0, 0], [2, 0]], [[1, 0]])
- Output
- [[-2], [0], [2]]
Identity-like: 2 components, 2D data preserved
- Input
- pca_project([[1, 0], [0, 1], [-1, 0]], [[1, 0], [0, 1]])
- Output
- [[1, 0], [0, 1], [-1, 0]]
Hints
Hint 1
Use a matrix product rather than nested loops, and check which operand transposes.
Hint 2
Do not forget to transpose of components. That step is easy to skip.
Requirements
X_centered: Centered data matrix of shape (n, d)components: Principal components of shape (r, d), one per rowReturn Projected data of shape (n, r).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Try similar problems(1)
Where this shows up
~15 min
••••••
3 employers weight this skill
2 big tech firms, 1 health and bio company. Top match scores 85.
Python
import numpy as np
def pca_project(X_centered: np.ndarray, components: np.ndarray) -> np.ndarray:
"""
Project centered data onto principal components.
Args:
X_centered: Centered data matrix of shape (n, d)
components: Principal components of shape (r, d), one per row
Returns:
Projected data of shape (n, r).
"""
# YOUR CODE HERE
pass