Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessons
Curves and trees · ML-R08 · 18–25 MIN

Validate polynomial flexibility

Choose degree inside the complete pipeline.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. PractiseAdapt the Python
  4. TransferCompute and inspect the evidence

Understand the idea

Expansion, scaling and Ridge belong together inside CV. Scaling after expansion makes regularisation act on comparable terms. Compare a curve with a line on identical folds. Ridge adds an L2 penalty that discourages large expanded-feature coefficients (alpha=1 by default); expansion creates the terms, scaling makes their numerical units comparable, and Ridge learns the prediction weights.

Illustrative data

Illustrative data
ColumnValues
x0, 1, 2, 3, 4, 5

Click the highlighted code to change the result.

model = Pipeline([
    ("polynomial", PolynomialFeatures(degree=, include_bias=False)),
    ("scale", StandardScaler()),
    ("model", Ridge()),
])
answer = model.named_steps["polynomial"].fit_transform(example[["x"]])
The polynomial step creates 2 power columns. This expands the inputs; it does not fit the prediction model.input xfeature xfeature x^200011122433944165525

The polynomial step creates 2 power columns. This expands the inputs; it does not fit the prediction model.

What the syntax does

model.named_steps['polynomial']
  • named_steps selects a Pipeline step by its declared name.
  • Here polynomial expands x into power columns.
  • Calling its fit_transform prepares those columns; the scale and prediction model steps have not been fitted.
  • The prepare step contains fitted preparation; model contains the prediction estimator.
  • A ColumnTransformer also exposes its named_transformers_ when inspecting encoded categories.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['x']]
y
Target series, aligned with X.
y = df.y
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CURVE48 · first 8 rows
xy
-315.9571
-2.8723413.0709
-2.7446814.9362
-2.6170214.45
-2.489369.39011
-2.36179.68978
-2.2340411.2101
-2.106389.96814
Your task
  1. Create the degree-two polynomial pipeline.
  2. Store the transformed training x and x² columns in answer, one row per training example.
answer
Hints

Think: Expansion belongs before scaling and the regularised estimator.

Tools: Pipeline, PolynomialFeatures, StandardScaler and Ridge.

Approach: Build the three named steps, then inspect the polynomial step’s training output.

Explained solution
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures,StandardScaler
from sklearn.linear_model import Ridge
model=Pipeline([('polynomial',PolynomialFeatures(degree=2,include_bias=False)),('scale',StandardScaler()),('model',Ridge())])
answer=model.named_steps['polynomial'].fit_transform(X_train)

The dimensions expose the representation entering later preparation; they do not by themselves establish predictive quality.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.