Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Regression lessonsQUESTIONS · MODELS · EVIDENCE
Curves and trees · ML-R08 · 18–25 MIN

Validate polynomial flexibility

Choose degree inside the complete pipeline.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. PractiseAdapt the Python
  4. TransferExplain the Python result

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.

Choose degree inside the complete pipeline.Curved featuresStraight-line candidateMore flexibility must earn its place in validation.
Schematic · Choose degree inside the complete pipeline.Scroll the diagram horizontally if needed.

Python skill: Addresses degree inside the pipeline step named polynomial.

Meet the syntax

GridSearchCV(model, {'polynomial__degree':[2,3]}, cv=folds, scoring='neg_root_mean_squared_error')
'polynomial__degree'
Addresses degree inside the pipeline step named polynomial.
[2,3]
Declares the candidate degrees; cross-validation chooses using training evidence.
cv=folds
Uses the same folds for both candidate feature expansions.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

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)

This practice: Read and run the Python. Next: Change · Validate polynomial flexibility.

Given data · CURVE48

48 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

CURVE48 · first 8 prepared 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

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
xfloat64
yfloat64
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.model_selection import train_test_split
X=df[['x']]
y=df.y
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42)

Your task · Follow

Create the degree-two polynomial pipeline. Store the transformed training x and x² columns in answer, one row per training example.

Hint 1 — Think

Expansion belongs before scaling and the regularised estimator.

Hint 2 — Tools

Pipeline, PolynomialFeatures, StandardScaler and Ridge.

Hint 3 — 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.

Helpful prior knowledge: Make curved features · How a search makes a choice These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Create the degree-two polynomial pipeline. Store the transformed training x and x² columns in answer, one row per training example.

answer

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.