📘
Polynomial Feature Expansion
MediumLinear Regression
Implement polynomial feature expansion for a single-feature input.
Given a list of values x and a degree d, create the expanded feature matrix where each row contains [xi,xi2,xi3,...,xid].
For example, if x=[2,3] and degree =3, the output matrix is: [2349827]
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] and degree d=3 are given, and we need to create the expanded feature matrix.
- For each value xi in the list, we calculate the powers of xi from 1 to d: xi1,xi2,...,xid. For x1=2, this gives us 21=2, 22=4, and 23=8.
- We repeat this process for x2=3: 31=3, 32=9, and 33=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]].
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
Python 3.13.1
Test Results
0/0Run code to see test results.