Problems
Loading...
1 / 1

Polynomial Features

Feature Engineering
Easy

Polynomial feature expansion transforms a single numeric feature into multiple features by raising it to successive powers. This allows linear models to learn non-linear relationships. For example, a quadratic relationship y = ax^2 + bx + c can be captured by a linear model if we provide [1, x, x^2] as features.

Given a list of values and a maximum degree d, generate the polynomial feature matrix where each row contains [x^0, x^1, x^2, ..., x^d].

Algorithm

For each input value x, compute all powers from 0 to d:

ϕ(x)=[x0,x1,x2,,xd]=[1,x,x2,,xd]\phi(x) = [x^0, x^1, x^2, \ldots, x^d] = [1, x, x^2, \ldots, x^d]

The x^0 = 1 term serves as the bias/intercept feature.

Loading visualization...

Examples

Input:

values = [2, 3], degree = 2

Output:

[[1, 2, 4], [1, 3, 9]]

For x=2: [2^0, 2^1, 2^2] = [1, 2, 4]. For x=3: [3^0, 3^1, 3^2] = [1, 3, 9].

Input:

values = [-2], degree = 3

Output:

[[1, -2, 4, -8]]

Powers of -2: [1, -2, 4, -8]. Odd powers are negative, even powers are positive.

Hint 1

For each value x, create a list using a loop or list comprehension: [x**p for p in range(degree + 1)]. Collect all rows into the result.

Hint 2

Remember that x**0 = 1 for any x (including 0). The range should go from 0 to degree inclusive, giving degree + 1 elements per row.

Requirements

  • For each value, generate powers from 0 to degree (inclusive)
  • Include x^0 = 1 as the first element (bias term)
  • Return a list of lists where each inner list has degree + 1 elements

Constraints

  • values has at least 1 element
  • degree >= 0
  • Return a list of lists of numbers
  • Time limit: 300 ms
Try Similar Problems