Polynomial Feature Expansion
~20 mincode completion
Implement poly_features(X) that returns an array with original + degree-2 terms.
Examples
1-feature input: only original and squared columns, no cross terms
- Input
- poly_features([[3]])
- Output
- [[3, 9]]
Hints
Hint 1
Join the pieces along the feature axis, not the row axis.
Hint 2
Do not forget to cross product terms. That step is easy to skip.
Requirements
X: 2D array of shape (n, d)Return 2D array of shape (n, d + d*(d+1)//2)
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~20 min
••••••••••••••••
8 employers weight this skill
3 AI product companies, 2 big tech firms, 2 enterprise vendors, 1 defense company. Top match scores 87.
Python
import numpy as np
def poly_features(X: np.ndarray) -> np.ndarray:
"""
Expand each row with degree-2 polynomial features.
Column order: [original features | squared terms | cross-product terms (i<j)].
Args:
X: 2D array of shape (n, d)
Returns:
2D array of shape (n, d + d*(d+1)//2)
"""
# YOUR CODE HERE
pass