PIXELBANKv8.2.1
Menu

Polynomial Feature Expansion

Implement polynomial feature expansion for a single-feature input.

Given a list of values xx and a degree dd, create the expanded feature matrix where each row contains [xi,xi2,xi3,...,xid][x_i, x_i^2, x_i^3, ..., x_i^d].

For example, if x=[2,3]x = [2, 3] and degree =3= 3, the output matrix is: [2483927]\begin{bmatrix} 2 & 4 & 8 \\ 3 & 9 & 27 \end{bmatrix}

Return the feature matrix as a 2D list, with each value rounded to 4 decimal places.

Example:

Input:
x = [2, 3]
degree = 3
Output:
[[2, 4, 8], [3, 9, 27]]
Reasoning:
  • The input list x=[2,3]x = [2, 3] and degree d=3d = 3 are given, and we need to create the expanded feature matrix.
  • For each value xix_i in the list, we calculate the powers of xix_i from 11 to dd: xi1,xi2,...,xidx_i^1, x_i^2, ..., x_i^d. For x1=2x_1 = 2, this gives us 21=22^1 = 2, 22=42^2 = 4, and 23=82^3 = 8.
  • We repeat this process for x2=3x_2 = 3: 31=33^1 = 3, 32=93^2 = 9, and 33=273^3 = 27.
  • The results are combined into a 2D list, where each row corresponds to the expanded features for each input value: [[2,4,8],[3,9,27]][[2, 4, 8], [3, 9, 27]].

Constraints:

  • x is a list of numbers, degree is a positive integer >= 1
  • Return a 2D list where row i has [x_i, x_i^2, ..., x_i^degree]
  • Round each value to 4 decimal places
  • Do NOT include a bias column of ones
Editor

Test Results

0/0
Run code to see test results.