Understand the idea
PolynomialFeatures constructs powers and interactions. The subsequent regression remains linear in those expanded features, while predictions can curve in the original inputs.
Illustrative data
| Column | Values |
|---|---|
x | 0, 1, 2, 3, 4, 5 |
Click the highlighted code to change the result.
expander = PolynomialFeatures(degree=, include_bias=False)
expanded = expander.fit_transform(example[["x"]])Degree 2 creates powers 1 through 2. The next lesson fits a model to these columns.
What the syntax does
PolynomialFeatures(degree=2, include_bias=False)- Creates powers and products of the input columns.
include_bias:False,Truedegree:1,2,4
degreeis a nonnegative integer; the default is 2.include_bias=False omits the all-ones feature when the later estimator supplies an intercept.- Greater degree adds flexibility and terms; use matching training folds and inspect extrapolation risk.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
x | y |
|---|---|
| -3 | 15.9571 |
| -2.87234 | 13.0709 |
| -2.74468 | 14.9362 |
| -2.61702 | 14.45 |
| -2.48936 | 9.39011 |
| -2.3617 | 9.68978 |
| -2.23404 | 11.2101 |
| -2.10638 | 9.96814 |
- Expand CURVE48
xintoxandxsquared.
answerHints
Think: A curved representation can be built before fitting a linear estimator.
Tools: PolynomialFeatures, degree and include_bias.
Approach: Fit the degree-two expansion to the single x column and inspect its transformed columns.
Explained solution
from sklearn.preprocessing import PolynomialFeatures
expander=PolynomialFeatures(degree=2,include_bias=False)
answer=expander.fit_transform(df[['x']])
Excluding the bias avoids a duplicate constant term; the two output columns carry the original input and its square.