Polynomial Feature Expansion
Polynomial Feature Expansion
Degree-2 polynomial features let a linear model learn non-linear relationships by explicitly adding squared and interaction terms. For a sample , the degree-2 expansion adds:
Generally for d original features the degree-2 expansion contains:
d original featuresIn this problem, produce the sorted expansion: original features first, then squared terms in column order, then cross-terms with in lexicographic column order.
Example: X = [[1, 2]]
Your task:
Implement poly_features(X) that returns an array with original + degree-2 terms.
Example Tests
2-feature row: 5 output columns
Input: {"X":[[1,2]]}
Expected: [1,5]
First row of expansion: original then squares then cross-term
Input: {"X":[[1,2]]}
Expected: [1,2,1,4,2]
1-feature input: only original and squared columns, no cross terms
Input: {"X":[[3]]}
Expected: [[3,9]]