Polynomial Feature Expansion

Easy
~20 min
code completion

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:

  • The d original features
  • terms: for each , plus for each
  • In 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]]

  • Original:
  • Squared:
  • Cross:
  • 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]]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.